-1

I have a 1x600 (rowsxcolumns) vector, say "A", where i want to access the following columns of A:

166   (column value is 2)
256   (column value is 5)
346   (column value is 8)
436   (column value is 10)
526   (column value is 13)

After extracting out these columns, I want to add their respective values:

sum = 2 + 5 +8 + 10 + 13

Can any one help me out on how to first extract the columns , and then sum up their values? Thanks!

Mokujin
  • 65
  • 6
  • See: http://www.mathworks.com/help/matlab/math/matrix-indexing.html and http://www.mathworks.com/help/matlab/ref/sum.html – mbauman Sep 17 '13 at 13:53

2 Answers2

1

Use

idxToSum = [166 256 346 436 526]; % or another way to give find your indices
yourSum = sum(A(idxToSum));
Nick
  • 3,143
  • 20
  • 34
0

Like this:

sum(A([166, 256, 346, 436, 526]))

e.g.

A = [5,4,3,2,1];

A([3, 5]) %// i.e. get the 3rd and 5th column

returns 3 1

so sum(A([3, 5])) returns 4

Dan
  • 45,079
  • 17
  • 88
  • 157