Finding two similar Matrix in a whole set

17 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-3-1
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 2023-3-1
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.

请先登录,再进行评论。

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-3-2
编辑:Dyuman Joshi 2023-3-2
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.

更多回答(2 个)

Bruno Luong
Bruno Luong 2023-3-1
编辑:Bruno Luong 2023-3-1
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 2023-3-1
编辑:Luca Ferro 2023-3-1
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 个评论
Luca Ferro
Luca Ferro 2023-3-1
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);

请先登录,再进行评论。

类别

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