How can I extract data and match it from two different data??

1 view (last 30 days)
Hello.
for example, I have two data.
1 is [ 0 0 0 0 1 1 1 0 0 0 ] and
2 is [ 5 5 5 5 10 10 10 6 6 6 ]
What I want to do is that first find a value '1' position in data 1* 5th,6th,7th) and extract a data from same position in data 2.(10 10 10 )
in original dat there are more then 5000 sequence... :(
Is there any way to do this?

Accepted Answer

the cyclist
the cyclist on 12 Feb 2020
Edited: the cyclist on 12 Feb 2020
If your first vector is only ones and zeros, then it is as simple as
idx = [ 0 0 0 0 1 1 1 0 0 0 ];
b = [ 5 5 5 5 10 10 10 6 6 6];
b(idx)
If the first vector has more general values, and you want the values of the second vector where the non-zeros are then
a = [ 0 0 0 0 2 1 6 0 0 0 ];
b = [ 5 5 5 5 10 10 10 6 6 6];
b(a~=0)
If you actually need the positions of the non-zero elements, then
pos = find(a);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!