MATLAB: How to check whether a vector belongs to a matrix

construct new matrixvector in matrix

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

Best Answer

Psteps=[P_1(1:end-1);P_1(2:end)].';
Q=ismember(N,Psteps,'rows').'
Related Question