How to check whether a vector belongs to a matrix?

13 views (last 30 days)
I have a vector of
P_1= [1 3 4 11 10 15 19 0 0 0 0 0 0 0 0 0 0 0 0 0]
and I want to see if each sequence of steps can be found in a larger matrix
N= [1 2
1 3
2 1
2 6
3 1
3 4
3 12
4 3
4 5
4 11]
.
I want to construct a new matrix Q that states if P_1(1:2) belongs to a row in N, then it should give a 1, otherwise 0. Therefore, I should get a Q matrix of [0 1 0 0 0 1 0 0 0 1]. I already have the following:
for z=1:19
for x= 1:10
if P_1(z:z+1)==N(x,1:2)
Q(x,1)=1
else Q(x,1)=0
end
end
end

Accepted Answer

Matt J
Matt J on 3 Jul 2019
Edited: Matt J on 3 Jul 2019
Psteps=[P_1(1:end-1);P_1(2:end)].';
Q=ismember(N,Psteps,'rows').'

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!