필터 지우기
필터 지우기

Question of a vlookup equivalent in matlab

조회 수: 2 (최근 30일)
Mike
Mike 2013년 11월 4일
댓글: Cedric 2013년 11월 6일
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
Matt Kindig
Matt Kindig 2013년 11월 4일
I'm not sure I really understand your question. Can you post some sample data and your expected output?
Mike
Mike 2013년 11월 5일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by