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!