-1

我有一个 1x600(rowsxcolumns)向量,比如“A”,我想在其中访问 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)

提取出这些列后,我想添加它们各自的值:

sum = 2 + 5 +8 + 10 + 13

任何人都可以帮助我如何首先提取列,然后总结它们的值吗?谢谢!

4

2 回答 2

1

利用

idxToSum = [166 256 346 436 526]; % or another way to give find your indices
yourSum = sum(A(idxToSum));
于 2013-09-17T13:56:14.957 回答
0

像这样:

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

例如

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

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

返回 3 1

所以sum(A([3, 5]))返回4

于 2013-09-17T13:54:13.810 回答