operation between 2 vectors by Indexing

2 visualizzazioni (ultimi 30 giorni)
Lila wagou
Lila wagou il 8 Apr 2017
Risposto: Star Strider il 8 Apr 2017
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

Risposte (1)

Star Strider
Star Strider il 8 Apr 2017
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

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by