How to add multiple values from column together

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?

답변 (1개)

Daniel Catton
Daniel Catton 2021년 1월 27일

0 개 추천

n = 10
a = rand(n,1);
b = sum(a);

댓글 수: 2

link
link 2021년 1월 27일
Hey, I do not want to add all values from, but only the values from the indices I provided in a second vector
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))+...

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2021년 1월 27일

댓글:

2021년 1월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by