Clear Filters
Clear Filters

Finding two similar Matrix in a whole set

14 views (last 30 days)
Hi Guys,
i have multiple Matrix and i want to compare them if some of them a similar to each other. Is there any command to do so? I mean i could just use == to compare two Matrix, but if I have 12 of them and compare every single one to another this would take me months...
  11 Comments
Dyuman Joshi
Dyuman Joshi on 1 Mar 2023
So you want to check if any of the two matrices are equal? What if there are more than 2 matrices that are equal?
Muhammed Ali
Muhammed Ali on 1 Mar 2023
Thats a good question. I dont know. What do you suggest? The output a can be different than i prefer, but the most important is, that you can see in the output which ones are exactly the same. This can be 2, 3, or 5 same matrices and as i mentioned before, there is no problem of having different types of outputs as long as its understandable.

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 2 Mar 2023
Edited: Dyuman Joshi on 2 Mar 2023
a=[3 4 5; 6 6 4];
b=[4 6 3; 2 1 3; 9 4 2];
c=[2 4 5; 6 8 5; 2 8 7];
d=[2 5 4;7 5 4];
e=a;l=a;
f=b;g=b;
h=randi(10,3,4);
k=c;
%defining cell array
arr={a,b,c,d,e,f,g,h,k,l};
% 1,2,3,4,5,6,7,8,9,10
num=numel(arr);
%pre-allocation
out=NaN(1,num);
for idx=1:num
for jdx=idx+1:num
if isequal(arr{idx},arr{jdx})
out(jdx)=min(idx,out(idx));
end
end
end
out
out = 1×10
NaN NaN NaN NaN 1 2 2 NaN 3 1
Here you can see which matrices are equal to which ones - 5th and 10th are equal to 1st, 6th and 7th are equal to 2nd and 9th is equal to 3rd.
However, manually defining the cell array is a limitation here. If you are getting the matrices as an output from some code, I strongly recommend you directly store them in cell array.

More Answers (2)

Bruno Luong
Bruno Luong on 1 Mar 2023
Edited: Bruno Luong on 1 Mar 2023
If you have great number of matrices (like 1000 or more) reduce the number of comparisons by computing for each matrix the min, max, mean, standard deviation, entropy, skewness, ......
If all that match then compare them with isequal

Luca Ferro
Luca Ferro on 1 Mar 2023
Edited: Luca Ferro on 1 Mar 2023
this is for similarity:
mtxs={[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)]};
allPairs=nchoosek(mtxs,2); %generates all pairs possible
[rows,~]=size(allPairs);
for jj=1:rows
pairsSimilarity{1,jj}=1-pdist2(allPairs{jj,1},allPairs{jj,2},'cosine'); %performs cosine similarity and puts it in a cell array
allPairs{jj,3}=pairsSimilarity{1,jj}; %appends similarity matrix to the cell pair in the same row
end
this is for equality:
mtxs={[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)]};
allPairs=nchoosek(mtxs,2); %generates all pairs possible
[rows,~]=size(allPairs);
for jj=1:rows
pairsEquality{1,jj}=isequal(allPairs{jj,1},allPairs{jj,2}); %performs equality and assigns to cell array
allPairs{jj,3}=pairsEquality{1,jj}; %appends equality bool to the cell pair in the same row
end
in both cases each row of allPairs has 3 cells:
  • cell1 and cell 2 are the matrixes of the pair
  • cell 3 is the result of the comparison
you can visualize a row by doing:
allPairs{n,:} %where n is the number of the row you want
you can access the result by doing:
allPairs{n,3} %where n is the number of the row you want
this is a third version if you want to check the equality of each element in the pair:
mtxs={[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)]};
allPairs=nchoosek(mtxs,2); %generates all pairs possible
[rows,~]=size(allPairs);
for jj=1:rows
pairsEquality{1,jj}=allPairs{jj,1}==allPairs{jj,2}; %performs element wise equality and assigns to cell array
allPairs{jj,3}=pairsEquality{1,jj}; %appends equality matrix to the cell pair in the same row
end
Note: you have to substitute the first line of code (mtxs) with a cell array of your 12 matrixes
  3 Comments
Luca Ferro
Luca Ferro on 1 Mar 2023
just integrate it with you code by copy pasting and formatting it so that it matches yours.
if you want a function out of it you can define it as such:
function allPairs=isSimilar(mtxs) %mtxs is a cell array of matrixes nxn
mtxs={[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[magic(4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)],[randi(4,4)]};
allPairs=nchoosek(mtxs,2); %generates all pairs possible
[rows,~]=size(allPairs);
for jj=1:rows
pairsEquality{1,jj}=allPairs{jj,1}==allPairs{jj,2}; %performs element wise equality and assigns to cell array
allPairs{jj,3}=pairsEquality{1,jj}; %appends equality matrix to the cell pair in the same row
end
end
call it by:
simRes=isSimilar(mtxs);

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!