필터 지우기
필터 지우기

Find in a matrix value pair.

조회 수: 3 (최근 30일)
Maurizio
Maurizio 2011년 12월 15일
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
Chandra Kurniawan
Chandra Kurniawan 2011년 12월 15일
I got little confused.
What does 'analyze the combination of this two value' means?
Can U tell me?
Maurizio
Maurizio 2011년 12월 15일
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.

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 12월 19일
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
Maurizio
Maurizio 2011년 12월 19일
Andrei can I order a 250x20cell based for example on the first two colums?
Fangjun Jiang
Fangjun Jiang 2011년 12월 19일
+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().

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

추가 답변 (1개)

the cyclist
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
Maurizio 2011년 12월 19일
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'.
the cyclist
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.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by