Find in a matrix value pair.
Mostrar comentarios más antiguos
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 comentarios
Jan
el 15 de Dic. de 2011
Using proper Matlab syntax is recommended. What exactly does "A|C|2" mean?
Chandra Kurniawan
el 15 de Dic. de 2011
I got little confused.
What does 'analyze the combination of this two value' means?
Can U tell me?
Maurizio
el 15 de Dic. de 2011
Respuesta aceptada
Más respuestas (1)
the cyclist
el 15 de Dic. de 2011
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 comentarios
Maurizio
el 15 de Dic. de 2011
Andrei Bobrov
el 15 de Dic. de 2011
x = randi(3,20,2)
[a b c] = unique(x,'rows')
out = [a histc(c,1:max(c))]
the cyclist
el 15 de Dic. de 2011
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
el 15 de Dic. de 2011
Maurizio
el 15 de Dic. de 2011
the cyclist
el 15 de Dic. de 2011
"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
el 16 de Dic. de 2011
Maurizio
el 16 de Dic. de 2011
Maurizio
el 16 de Dic. de 2011
the cyclist
el 16 de Dic. de 2011
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
el 17 de Dic. de 2011
the cyclist
el 17 de Dic. de 2011
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
el 17 de Dic. de 2011
the cyclist
el 18 de Dic. de 2011
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
el 19 de Dic. de 2011
the cyclist
el 19 de Dic. de 2011
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
el 19 de Dic. de 2011
Maurizio
el 19 de Dic. de 2011
Maurizio
el 19 de Dic. de 2011
the cyclist
el 19 de Dic. de 2011
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.
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!