Comparing the data and Pairing a data - Problem occurs - Needed help

2 次查看(过去 30 天)
Hello all, inorder to do MRC i need to do a pairing process as per my project
Consider some vectors
a1 =[1 2 3 4 5 6 7 8]'; b1= [3 3 2 3 2 3 3 2]'; c1=zeros[8,1];
I need to check b1(1)==3 if it is 3 then allocate a1(1) value in c1(1),
then i need to check b1(2)==3 if it is 3 then allocate a1(1) (SAME VALUE OF a1(1)) value in c1(2).
Now c1(1) & c1(2) = a1(1) ie. 1
Like this where ever 3 comes i need to take 3 as a pair and respective values to be repeated twice.
Then I need to check b1(3)==3 if it is not equal to 3 then allocate a1(2) (NEXT VALUE OF a1 vector) value in c1(3)
Now c1(3)= a1(2) ie.2
Then i need to check b1(4)==3 if it is equal then allocate a1(3) (NEXT VALUE OF a1 vector) value in c1(4)
Now c1(4)=a1(3) ie.3
Then i need to check b1(5)==3 if it is not equal to 3 then allocate a1(4) (NEXT VALUE OF a1 vector) value in c1(5)
Now c1(5)=a1(4) ie.4
Then i need to check b1(6)==3 if it is equal then allocate a1(3) (SAME VALUE OF a1 vector which is not paired before) value in c1(6)
Now c1(6)=a1(3) ie.3
c1(4) & c1(6)=a1(3) ie. 3 should be allocated
this is the code i have written
for i=1:2:8
for j=i:i+1
if b1(j)==3
c1(j)=a1(i);
else
c1(j)=a1(i);
end
end
end
MY EXPECTED OUTPUT IS c1=[1 1 2 3 4 3 5 6]'
The output I am getting is c1 =[ 1 1 3 3 5 5 7 7]'
NOTE: In my program vector b1 is randomised with the values 2,3 so i cannot predict the position.
Kindly suggest a solution to my problem, Thanks in advance.

回答(0 个)

类别

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

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by