Borrar filtros
Borrar filtros

Question of a vlookup equivalent in matlab

4 visualizaciones (últimos 30 días)
Mike
Mike el 4 de Nov. de 2013
Comentada: Cedric el 6 de Nov. de 2013
I dont think a vlookup function exists in matlab, but can you guys give me some pointers on how to replicate it? I have a matrix A and vector B, and I want to extract the rows in matrix A into a new matrix C if the A(:,1) is found in vector B. Will I need a loop?
I created C = A( A(:,1) == B(1:end) ,:), but I have a matrix dimesions disagreement. Is there a way to loop through each element of B?
  2 comentarios
Matt Kindig
Matt Kindig el 4 de Nov. de 2013
I'm not sure I really understand your question. Can you post some sample data and your expected output?
Mike
Mike el 5 de Nov. de 2013
So if A =[ 1 2 3 4 ; 4 9 8 0 ; 8 7 8 9] and B is a vector = [ 1 8] I want a matrix C such that C = [ 1 2 3 4; 8 7 8 9]. Is this better?

Iniciar sesión para comentar.

Respuesta aceptada

Cedric
Cedric el 5 de Nov. de 2013
Try this:
>> id = ismember(A(:,1), B)
id =
1
0
1
>> C = A(id,:)
C =
1 2 3 4
8 7 8 9
  5 comentarios
Andrei Bobrov
Andrei Bobrov el 6 de Nov. de 2013
[la,idx] = ismember(B,A(:,1));
C = A(idx(la),:);
Cedric
Cedric el 6 de Nov. de 2013
Yes, I assumed them to be sorted based on the numeric example, but I shouldn't have! Thank you for the comments.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by