Find in a matrix value pair.
이전 댓글 표시
I have a matrix with 8 column and 250 raw. I need to create a function that read the first two column and and analyze the combination of this two value. For Example the matrix is
A|B|...
A|C|...
C|A|...
A|C|...
C|A|...
and I would like to have as output the single value pairs and the repetitions number . For example : A|B|1
A|C|2
C|A|2...
Could you help me please?
댓글 수: 3
Jan
2011년 12월 15일
Using proper Matlab syntax is recommended. What exactly does "A|C|2" mean?
Chandra Kurniawan
2011년 12월 15일
I got little confused.
What does 'analyze the combination of this two value' means?
Can U tell me?
Maurizio
2011년 12월 15일
채택된 답변
추가 답변 (1개)
the cyclist
2011년 12월 15일
I think I understand what you want to do. It can be done in three steps:
- Isolate the first two columns of your array:
>> x = A(:,[1 2]);
- Find the unique two-element combinations (i.e. unique rows):
>> [ux,i,j] = unique(x,'rows')
- Find the frequency count of the indices to those rows:
>> count = hist(j,unique(j))
I was not able to test this out, so you should think it through and test it, but I think those are the basic elements.
댓글 수: 20
Maurizio
2011년 12월 15일
Andrei Bobrov
2011년 12월 15일
x = randi(3,20,2)
[a b c] = unique(x,'rows')
out = [a histc(c,1:max(c))]
the cyclist
2011년 12월 15일
Maurizio, my suggestion is that rather than blindly taking the code and see if it does what you want, you instead try to understand the reasoning behind it. Read the help files for unique() and hist(). I am sure this is very close to the solution that you need, even if it isn't perfect yet.
Maurizio
2011년 12월 15일
Maurizio
2011년 12월 15일
the cyclist
2011년 12월 15일
"ux" is the unique rows of "x".
You don't need "i".
Every row of x appears in ux. For a given row of x, in which row of ux does it appear? That's what "j" tells you. So, tally up the frequency of j [using hist()], and you know the frequency of the rows of x.
Maurizio
2011년 12월 16일
Maurizio
2011년 12월 16일
Maurizio
2011년 12월 16일
the cyclist
2011년 12월 16일
Here is a small snippet of code, based exactly on my solution, that does exactly what you want:
A = [1 2; 3 4; 3 4; 5 6; 1 2];
x = A(:,[1 2]);
[ux,i,j] = unique(x,'rows')
count = hist(j,unique(j))
out = [ux count']
"out" has three columns: The first column is the first value of the unique pair. The second column is the second value of that unique pair. The third column is the number of times the unique pair occurs in A.
Did you maybe forget to put the second argument 'rows' into the unique() command?
Maurizio
2011년 12월 17일
the cyclist
2011년 12월 17일
Ah. You said you had a matrix, so I assumed you had numeric values.
If you are trying to do this for a cell array, then I suggest you download the following function from the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/25917-unique-rows-for-a-cell-array
That will allow you to find the unique rows from a cell array, which the MATLAB function unique() will not do.
Maurizio
2011년 12월 17일
the cyclist
2011년 12월 18일
The command from the file exchange has the exact same output format as the unique() command from MATLAB. Therefore you can still use the hist() command to get the counts, exactly as in my example.
Maurizio
2011년 12월 19일
the cyclist
2011년 12월 19일
Don't you get an "i" and "j" vector [just like with MATLAB's unique()] when you call the function like this:
[ua,i,j]=uniqueRowsCA({'a','a';'a','b';'a','a'})
You should get i==[3;2] and j==[1;2;1] in this example.
If not, it is possible I sent you a link to the wrong file.
Maurizio
2011년 12월 19일
Maurizio
2011년 12월 19일
Maurizio
2011년 12월 19일
the cyclist
2011년 12월 19일
As I said in a prior comment, you have to download that function from here: http://www.mathworks.com/matlabcentral/fileexchange/25917-unique-rows-for-a-cell-array
Then put that function in your working directory, or somewhere in your path.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!