# question on indexing: How to extract rows from a matrix that crossponds to certain values in another vector?

2 visualizaciones (últimos 30 días)
Cantor Set el 15 de Feb. de 2020
Respondida: Stephen23 el 15 de Feb. de 2020
Ro =
1 1 1 1
0 1 0 1
1 0 0 0
1 1 0 1
1 0 0 0
Given an arbitart matrix like this such that for every row in Ro crossponds a rank in ranks:
ranks=[1;2;3;4;3]
I need to extract all the rows in Ro that crossponds to ranks=3 but I need some general rule because the matrices Ro and ranks changes in the code
I thought of the following:
function [ pos ] = Position( x,n )
%x is the input row vector
%n is the number (in x) that we want to find its position(s) vector w.r.t
%x
pos=find(x==n);
end
FF=[];
P=Position(ranks,3);
lenP=length(P);
for i=1:lenP
FF=[FF;Ro(P(i),1:end)]
end
The problem is that FF=[1 0 0 0; 1 0 0 0];
I need it only be FF=[ 1 0 0 0]; I don't want repition
so can you help :)?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (2)

JESUS DAVID ARIZA ROYETH el 15 de Feb. de 2020
FF=unique(Ro(ranks==3,:),'rows')
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Stephen23 el 15 de Feb. de 2020
Just call find with its optional 2nd and 3rd input arguments to specify that you only want one result returned:
>> n = 3;
>> pos = find(ranks==n, 1, 'first');
>> FF = Ro(pos,:)
FF =
1 0 0 0
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Matrix Indexing 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