MATLAB Answers

Using find to compare matrices

조회 수: 3(최근 30일)
MiauMiau
MiauMiau 12 Dec 2012
Hi
I have a matrix where the first column contains id's and the second some sort of information about thes id's, as an example such as below. What I want to do now, is to be able to say which information belongs to which id, for instance, for the example below, the id 1 has the "values" 5 and 9. So I would like to write code such that I could return for each id it's values. Now I have written that:
Y = [1,2,3,3,1; 5,6,7,8,9]'
U = unique(Y(:,1))
for i= 1:length(U)
[A B] = find(U(i)==Y(:,1))
end
Where the column with the 1,2,3,3,1 is storing these id's. Does that make any sense? What do I actually get returned in the [A B]?
Thanks a lot

  댓글 수: 0

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

채택된 답변

Thomas
Thomas 12 Dec 2012
편집: Thomas 12 Dec 2012
You do not need find
Y = [1,2,3,3,1; 5,6,7,8,9]'
Y((Y(:,1)==1),2) % id =1
Y((Y(:,1)==2),2) % id=2
Y((Y(:,1)==3),2) % id=3

  댓글 수: 4

표시 이전 댓글 수: 1
Thomas
Thomas 12 Dec 2012
This should get all the values
Y = [1001,1010,1011,1011,1001; 5,6,7,8,9]'
U=unique(Y(:,1))
for ii=1:numel(U)
Y((Y(:,1)==U(ii)),2)
end
MiauMiau
MiauMiau 12 Dec 2012
Is that not almost the same thing, as I did in the very beginning if I may ask? What is returned to [A B] in my case?
Thomas
Thomas 12 Dec 2012
In your case it give the row and column number as answers to the find
eg for id=1
you get
A =
1
5
B =
1
1
i.e. 1 is present in y in row 1 column 1 and row 5 column 1
the generic form of find is
[row,col] = find(X, ...)

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

추가 답변(2개)

MiauMiau
MiauMiau 12 Dec 2012
Thanks very much!

  댓글 수: 0

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


MiauMiau
MiauMiau 12 Dec 2012
hi I have realized it still does not do what I wanted. Here I just get "7 8" as outputs. But I want to be able to get the values of each id. This means:
1001 -> 5 1010 -> 6
etc. etc.

  댓글 수: 0

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by