How to add multiple values from column together
3 ビュー (過去 30 日間)
古いコメントを表示
Lets assume I have a vector a with n values
a = rand(n,1) % values
and I priovide another vector b containing some row indices of the vector above
b = [1 3 8 9 11 15 ..] % indices from vector a
How can I add the values from these rows together without using a loop?
0 件のコメント
回答 (1 件)
Daniel Catton
2021 年 1 月 27 日
n = 10
a = rand(n,1);
b = sum(a);
2 件のコメント
Daniel Catton
2021 年 1 月 27 日
Oh I get you now, my bad. I thing you're going to struggle without using a loop. I can't think of a way without just coding the way a loop works. i.e:
a = rand(n,1); % values
b = [1 3 8 9 11 15 ..] % indices from vector a
a(b(1))+a(b(2))+a(b(3))+...
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!