How to compare and find the differences in each cell inside a cell structure array?

Hi there,
Originally I have a matrix size of 120x8 in my workspace. Assume it is named 'A1'.
Each line/row in the matrix is unique with no repeats. (Basically like truth table)
But for my calculation, I regenerated it with more variables, and enlarge the entire matrix, becoming a 960x8 matrix.
Then I use 'mat2cell' and reform them in square size, creating a cell array with 1x120 cells, with each cell contains an 8x8 matrix.
So now I have 1x120 cell array with each cell an 8x8 matrix within.
But then, some cells maybe the same, some maybe the same but in a different order.
How do I distinguish the different matrices and eliminate any similar ones?
Thank you very much.

4 件のコメント

Adam Danz
Adam Danz 2019 年 4 月 22 日
"But then, some cells maybe the same, some maybe the same but in a different order.'
Could you provide an example of the second part of that sentence?
Moses Tang
Moses Tang 2019 年 4 月 22 日
yeah sure.
An example could be an 4x4 truth table
1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1
Then a possible case In one of the cell in the matrix,
1 -1 1 -1 (row 2 of original)
1 -1 -1 1 (row 4 of original)
1 1 1 1 (row 1 of original)
1 -1 1 -1 (row 2 of original)
As you see, they are the same matrix with the same rows but in a different order.
Adam Danz
Adam Danz 2019 年 4 月 22 日
I see. And you want to eliminate all such duplicates, correct?
Moses Tang
Moses Tang 2019 年 4 月 22 日
Yes. Thats right !

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

 採用された回答

Adam Danz
Adam Danz 2019 年 4 月 22 日
編集済み: Adam Danz 2019 年 4 月 22 日
See the comments within the solution for an explanation of the methods. The basic approach is to convert your data to binary format ( [-1,1] to [0,1] ) and then to decimal format using bi2de(). That way each row of [-1 -1 1 1] values is represented by a single value (eg: bi2de([0 0 1 1]) = 12 ). Then we just need to search for cell arrays with the same sorted vectors.
% Create fake data to work with
A1{1} = 2*(rand(8)>.5) - 1;
A1{2} = 2*(rand(8)>.5) - 1;
A1{3} = 2*(rand(8)>.5) - 1;
A1{4} = 2*(rand(8)>.5) - 1;
A1{5} = A1{1}; %Repeat of {1}
A1{6} = 2*(rand(8)>.5) - 1;
A1{7} = A1{2}; %Repeat of {2}
A1{8} = 2*(rand(8)>.5) - 1;
A1{9} = A1{3}([1 8 2 7 3 6 4 5],:); %Repeat of {3} and different row order
A1{10} = 2*(rand(8)>.5) - 1;
% Temporarily replace -1 with 0 to produce binary values
A1bin = cellfun(@(x)(x+1)/2, A1, 'UniformOutput', false);
% Convert from binary to decimal, sorted (result will be a vector for each cell element)
A1dec = cellfun(@(x)sort(bi2de(x)), A1bin, 'UniformOutput', false);
% Convert to a matrix and find matching columns
A1mat = cell2mat(A1dec);
[~, unqRowIdx] = unique(A1mat', 'rows','stable');
% Note: In the fake data, elements 5, 7, 9 are duplicates
% The unqRowIdx should list all other elements except those.
% Extract unique cell elements using the column idx
A1unq = A1(unqRowIdx);
Note, bi2de() requires the Communications toolbox. If you don't have this toolbox, you can use bin2dec() instead:
A1dec = cellfun(@(x)sort(bin2dec(num2str((x)))), A1bin, 'UniformOutput', false);

2 件のコメント

Moses Tang
Moses Tang 2019 年 4 月 22 日
It is really interesting on how you construct the answer.
I have a question, actually im not sure whether I installed that specific toolbox.
But, I can still run both lines of function in the program and there are answers out.
However they produce different answers....
I thought they should all output as the same answer.
Do you know why?
Adam Danz
Adam Danz 2019 年 4 月 22 日
編集済み: Adam Danz 2019 年 4 月 22 日
Are you using the same input data? If you're running my code that generates a fake dataset, those data are random so they will produce different inputs each time (unless you set the rng seed).
I tested both lines with the same input data (without regenerating a new random dataset) and they produced identical results.
"im not sure whether I installed that specific toolbox",
you can run this line of code which produces the following output that indicates the toolbox (comm = communications toolbox).
which('bi2de')
% ans: C:\Program Files\MATLAB\R2018a\toolbox\comm\comm\bi2de.m
% or
% ans: 'bi2de' not found. (<- you don't have this function)

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2018b

質問済み:

2019 年 4 月 22 日

編集済み:

2019 年 4 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by