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 Comments

Jan
Jan on 15 Dec 2011
Using proper Matlab syntax is recommended. What exactly does "A|C|2" mean?
I got little confused.
What does 'analyze the combination of this two value' means?
Can U tell me?
I have an array with 25 column. I need to check if in the first two colums there are any combination and if yes I need to know for each combination the frequency.

Sign in to comment.

 Accepted Answer

k = { 'bc' 'ec'
'ed' 'cd'
'dc' 'ec'
'bc' 'be'
'ed' 'cd'
'ed' 'ae'
'bc' 'ec'
'ba' 'bb'
'ca' 'aa'
'ab' 'ad'}
[a,c,c] = unique(k);
B = reshape(c,size(k));
[N,M,M] = unique(B,'rows');
p = histc(M,1:max(M));
out = [a(N),num2cell(p)];

2 Comments

Andrei can I order a 250x20cell based for example on the first two colums?
+1. andrei, very nice way to use unique(CellArray,'rows') and avoid "Warning: 'rows' flag is ignored for cell arrays.". I used to combine cell array into char array and then apply unique().

Sign in to comment.

More Answers (1)

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 Comments

Thanks cyclist. the first step is correct. It's almost ok but with these three program lines I found how many times there is the single array. I would like to know how many times I have the combination of the two Value, for example A&B A&C C&A . Where A,B,C are the value of the array columns.
x = randi(3,20,2)
[a b c] = unique(x,'rows')
out = [a histc(c,1:max(c))]
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.
could you explain me the output please?
Cyclist..thanks for the help..I know hist is histogram but I need to understand unique how is working..Is not that I'm using the code and that it...is the opposite really.Ok I understand the first and the third line but the second I think I need to do something more..
with your code with ux I have the variable, I'm not able to understand i and J really
"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.
cyclist I think that your code work in case of matrix but in case of 2 colums array is not correct. Let me explain what I have as output on the 3 step that you suggested to me. With the first one I had as output to colums array (string no value).
With the second step as ux I have only the list of the variable not the combination, as J I have the a lot of 2 and 1 in a single column. and when I'm using hist of j I have not the combination of the pair but how many times I have the single value. For example I have six value on the two x columns. as first output I have 250 that is the value of the repetitions of this variable, as second output 248 that is the value of the repetitions of this variable and so on.. What I need is the number of the repetitions of the pair.
Is it possible to have if as input I have string and not value?
cyclist I think that your code work in case of matrix but in case of 2 colums array is not correct. Let me explain what I have as output on the 3 step that you suggested to me. With the first one I had as output to colums array (string no value).
With the second step as ux I have only the list of the variable not the combination, as J I have the a lot of 2 and 1 in a single column. and when I'm using hist of j I have not the combination of the pair but how many times I have the single value. For example I have six value on the two x columns. as first output I have 250 that is the value of the repetitions of this variable, as second output 248 that is the value of the repetitions of this variable and so on.. What I need is the number of the repetitions of the pair.
Is it possible to have if as input I have string and not value?
@ andrei bobrov. Could you please explain me how can I use your code in case I have as input not a matrix but two colums array please?
Thanks a lot
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?
Ok for the value this is working very good and is perfect. now with the x=A(:,[1 2]); I have 250x2Cell. on Each cell I have name for example on x{1,1}='pippo' and x{1,2}='pluto' and so on for each rows. When I'm using the second line of your code the ux='pippo' 'pluto'...(but not the combination) and then with the last row the output is the number of time that I have pippo or pluto but not the combination of the two values.
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.
ok I will find the unique rows but I will not be able to counter. Is it correct?
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.
How I can do? with unique I have the output in function of j. With the command from the file exchange I have only the combination and that's it.
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.
ok i will try because on the command from the file exchange is :
ii = 1;
while ii<size(A,1)
tf = true(size(A,1),1);
for jj = ii:size(A,1)
if isequal(A(ii,:),A(jj,:)) && ii~=jj
tf(jj) = 0;
end
end
A = A(tf,:);
ii = ii + 1;
end
For example, try it with:
A = {1,'red',magic(3);'blue',magic(4),'green';1,'red',magic(3)}
cyclist where I can find please the uniqueRowsCA function?
because with cyclist my {x} is a <250x2>cell and on each cell there are only name and I use [ua,i,j]=uniqueRowsCA(x) matlab give to me the following error: ??? Undefined function or method 'uniqueRowsCA' for input arguments of type 'cell'.
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.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!