How to check whether a vector belongs to a matrix?

13 visualizaciones (últimos 30 días)
Sa Sa
Sa Sa el 3 de Jul. de 2019
Comentada: Sa Sa el 3 de Jul. de 2019
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

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by