operation between 2 vectors by Indexing

조회 수: 2 (최근 30일)
Lila wagou
Lila wagou 2017년 4월 8일
답변: Star Strider 2017년 4월 8일
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