フィルターのクリア

Find match for a pair of rows in cell arrays

2 ビュー (過去 30 日間)
Stefano Alois
Stefano Alois 2016 年 9 月 29 日
コメント済み: dpb 2016 年 9 月 29 日
I have 2 cell arrays of equal length, e.g.
>> alfa = {'a'; 'b'; 'c'; 'b'; 'a'; 'c'}
alfa =
'a'
'b'
'c'
'b'
'a'
'c'
>> beta = {'b'; 'd'; 'a'; 'd'; 'a'; 'a'}
beta =
'b'
'd'
'a'
'd'
'a'
'a'
and I want to find where the pairs are repeated, which pair is it and the index where the repeated pairs are located.
So, in this case the index 2 and 4 correspond to the repeated pair 'b' and 'd', and the index 3 and 6 correspond to the repeated pair 'c' and 'a'. It is important that I know which pair is it and all the indexes where this pair is repeated.
Thank you.
  1 件のコメント
Adam
Adam 2016 年 9 月 29 日
What is the significance of having two cell arrays? I don't see what the relevance of the 2nd one is other than just as another example. Is it supposed to be used in some way?

サインインしてコメントする。

採用された回答

dpb
dpb 2016 年 9 月 29 日
編集済み: dpb 2016 年 9 月 29 日
Adam's on the right track, just didn't quite recognize the problem description--
>> ab=[char(alfa) char(beta)]; % mush the two arrays together
>> [u,ia,ib]=unique(ab,'rows'); % get the combinations that are extant
>> u(histc(ib,unique(ib))>1,:) % find the ones with more than one occurrence
ans =
bd
ca
>>
The alternate returns from histc and unique will provide the positions.

その他の回答 (1 件)

Adam
Adam 2016 年 9 月 29 日
編集済み: Adam 2016 年 9 月 29 日
[C, ia, ic] = unique( alfa );
should give you this information. The C vector gives the characters and the ic vector tells you where they occur so repeated indices show the locations of the same letter.
[C, ia, ic] = unique( alfa )
C =
'a'
'b'
'c'
ia =
1
2
3
ic =
1
2
3
2
1
3
  3 件のコメント
Adam
Adam 2016 年 9 月 29 日
So what do you have? 2 arrays or 1 array of 6x2 size?
dpb
dpb 2016 年 9 月 29 日
He stated he had two cell arrays only you must consider them together by row--see my response. Simplest way is as done there to convert to a 1D character vector as the 'rows' option doesn't work for cellstr arrays, unfortunately. (Why not is anybody's guess... :( )

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by