How to combine two vectors? HOW TO SPLIT VECTORS ACCORDING TO INDEX?

2 views (last 30 days)
I have a vector say A = [60,600,15]
I extract any value from this vector A using its index
Eg: If i extract A(2) from A. Then i create a vector B of same length as of A
and place that value in its respective index!
And the left over values will be in vector C = [60,15] OF SAME LENGTH AS A
that is B(2) = 600 and C(1) = 60 and C(3) = 15
*THE FINAL OUTPUT SHOULD BE LIKE B = [0,600,0] AND C = [60,0,15];
NOW USING VECTOR B AND C HOW TO COMBINE THEM TO OBTAIN VECTOR D = [60,600,15]*
CASE 2: IF I EXTRACT A(1) AND A(2)
THEN VECTOR B = [60,600,0] AND THE LEFT OVER VALUE FROM A SHOULD CREATE VECTOR C = [0,0,15]

Accepted Answer

Ameer Hamza
Ameer Hamza on 27 May 2018
  2 Comments
Ameer Hamza
Ameer Hamza on 27 May 2018
Edited: Ameer Hamza on 27 May 2018
Suppose you define a logical array inB to define which element goes to B
A = [60,600,15];
inB = logical([0 1 0]); %<--- 1 means go to B, 0 mean goes to C
B = zeros(size(A));
B(inB) = A(inB);
C = zeros(size(A));
C(~inB) = A(~inB);

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!