operation between 2 vectors by Indexing

1 次查看(过去 30 天)
Dear, I looking to doing a operation (summation for example) between two vectors but the elements indexing are stored in others two vectors:
vectors to summation
A = [10 25 12.5 4 -7.5];
B = [0.05 33.5 -20 12.25 0.65];
Indexing vectors
Aind = [1 4 5 4]; % index for A vector
Bind = [3 2 1 5]; % index for B vector
for result i must have
C(1) = A(1)+B(3) = 10+(-20)
C(2) = A(4)+B(2) = 4+33.5
C(3) = A(5)+B(1) = -7.5+0.05
C(4) = A(4)+B(5) = 4+0.65
I am blocked
for i = 1 : length(Aind)
C(i) = A(Aind ?)+B(Bind ?)
end

回答(1 个)

Star Strider
Star Strider 2017-4-8
No loop necessary. You have all the infromation you need in ‘A’, ‘B’, and the index vectors:
C = A(Aind) + B(Bind)
C =
-10 37.5 -7.45 4.65

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by