Ismember to replace value in vector?

조회 수: 6 (최근 30일)
Jona
Jona 2017년 12월 21일
댓글: Jona 2017년 12월 21일
Hi
I have created matrix B, see a cut out below (it is actually 260 rows long). Matrix A is the result of calculation (also here a cut out since it is 8637 rows long).
Now I want to find where the rows of B are found in A. I use ismember for this, but the problem is that ismember gives me the value 1. I want ismember to find the index like this, but to give it the same number as the row.
I did this:
B=[0 1 1; 0 1 2; 0 1 3];
A=[0 1 4; 0 2 4; 0 1 2; 0 1 5; 0 1 3];
output=zeros(5,1);
out1 = ismember(A,B(1,:),'rows')
out2 = ismember(A,B(2,:),'rows')
out3 = ismember(A,B(3,:),'rows')
I would like my function to return:
new_output => [0; 0; 2; 0; 3]
Later I will have to loop this 260 times (for each row), but for now I need to find a way to assign a value to each index.
Any suggestions on how to do this?
Thank you in advance

채택된 답변

Stephen23
Stephen23 2017년 12월 21일
편집: Stephen23 2017년 12월 21일
Read the ismember help, the second output is all you need:
>> [~,idx] = ismember(A,B,'rows')
idx =
0
0
2
0
3
>>
This is why we tell people to read the help for every function that they use.
  댓글 수: 1
Jona
Jona 2017년 12월 21일
Thank you, Stephen! It works. I will read the help more carefully next time. I just assumed that ismember was not the correct function, so I was already looking for something else.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by