how to get common elements within a cell array?

hey all
If i have following array:
{[1,7];[2,6,1];[7,1,3];[3,5]}
how can I compare each cell in this array with all other cells'values. for example for first cell [1,7] i have to compare it with all other cells and find common values. to compare with [2,6,1] we will get 1, to compare with [7,1,3] we will get [1,7] and with [3,5] we will get an empty cell. Similarly after this we will compare 2nd cell [2,6,1] with all cells in array.
please help

5 件のコメント

Jan
Jan 2018 年 3 月 5 日
What is the wanted output for this example?
Tha saliem
Tha saliem 2018 年 3 月 5 日
Actually i want intersection of the results of all comparisons for one cell. Like comparison of cell 1 gives
comparisonResult{1} = {[1],[1,7],[]}
and final result for cell 1 is:
finalResult{1} ={[1,7]} (if either 1 or 7 exists more than 1 time in comparisonResult, we will write it once).
Similarly for cell 2:
comparisonResult{2} = {[1],[1],[]}
FinalResult{2}={1}
Greg
Greg 2018 年 3 月 5 日
Did you try intersect? Probably need a loop or cellfun to go with it.
Jan
Jan 2018 年 3 月 5 日
編集済み: Jan 2018 年 3 月 5 日
Do the single vectors have unique elements? In other words: Is this possible as input or not:
{[1,7]; [2,6,1,1,6]}
Tha saliem
Tha saliem 2018 年 3 月 6 日
Yes they have unique elements.. [2,6,1,1,6] is not possible

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

 採用された回答

the cyclist
the cyclist 2018 年 3 月 6 日

0 投票

Do you need the comparison result? If not, I think this skips right to the final result
aL = 1:length(a);
for na = aL
finalResult{na} = intersect([a{na}],[a{setdiff(aL,na)}]);
end
This code is a bit obfuscated. But if it gives the result you expect, I can try to help you understand it if you need me to.

3 件のコメント

Tha saliem
Tha saliem 2018 年 3 月 6 日
Thanks alot for your time. yes it gives desired results. I am trying to understand it. confused about this part code [a{setdiff(aL,na)}].
and what can i do to achieve comparison result too?
the cyclist
the cyclist 2018 年 3 月 6 日
Here is the de-obfuscated version of my code, commenting on what each piece does.
% The data
a = {[1,7];[2,6,1];[7,1,3];[3,5]};
% Vector from 1 to the length of a
aL = 1:length(a);
% Loop through the vector
for na = aL
% Get the elements from the current cell
elementsFromCurrentCell = [a{na}];
% Create index to all *other* cells, except for the current cell
indexToOtherCells = setdiff(aL,na);
% Concatenate all the elements from those other cells
elementsFromAllOtherCells = [a{indexToOtherCells}];
% Find the intersection between the current cell and all the other cells
finalResult{na} = intersect(elementsFromCurrentCell,elementsFromAllOtherCells);
end
In order to get the "comparison" results, you just need to loop over each other cell in turn, rather than concatenating them all together the way I did to get the final result.
Tha saliem
Tha saliem 2018 年 3 月 6 日
Well Thank you so much for this detailed explanation. I have completely understood it. Thanks alot

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by