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

Jonas
Jonas 2023년 3월 1일
do you mean equal when you say similar?
equal
A simple approach would be to store the matrices in a cell array, access them using the required indices and use isequal() to comparing equality. Ideally, you should use tolerance to compare numeric values.
Hello Muhammed Ali,
You can follow this approach which was also given by Dyuman Joshi, You can go through the code given below.
%multiple matrixes
a=[1 2 7;3 4 8];
b=[10 4;3 6];
c=[5 6;3 4];
d=[1 2 7;3 4 8];
e=[11 22 55;6 7 10;8 15 17];
%concatenate all the matrices in to cell
f={a,b,c,d,e};
%run a for loop to compare all the possible combinations
out=0;
k=1;
for i=1:length(f)-1
for j=i+1:length(f)
if (isequal(f{i},f{j}))
out(k)=1;
else
out(k)=0;
end
k=k+1;
end
end
When you are working on set of matrixes which might have different sizes, Its better to use isequal compared to '==' because '==' throws an error if size of matrixes are different.
Luca Ferro
Luca Ferro 2023년 3월 1일
isn't this pretty much the same as my solution?
Jan
Jan 2023년 3월 1일
@Muhammed Ali: What is the wanted output? Do you want a set of unique matrices? Do you want a list of indices for each repeated matrix pointing to the other duplicates?
Hi Jan. I created a bunch of matrices. Now I want to compare them and find the matrices, which are exactly the same to eachother.
Jan
Jan 2023년 3월 1일
편집: Jan 2023년 3월 1일
@Muhammed Ali: Again, what is the wanted output? A logical vector, in which each repeated matrix has a TRUE, while others are FALSE? Or do you want a cell array of only the matrices, which are unique and all duplicates are removed? Or do you want a list of indices for each matrix, which contain the indices of the duplicates?
Please give a short example of the input and wanted output with e.g. 4 or 5 matrices.
"if I have 12 of them and compare every single one to another this would take me months." - avoid such exaggerations. Do you have millions of matrices, which have sizes 12x12, or hundreds of matrices, which have millions of elements?
For a lot of small matrices, it is cheap to calculate the MD5 sum of each matrix and store it in a cell string. Then Matlab's unique() solves the problem efficiently. But for a small number of huge arrays loops and isequal is faster.
The less details have to be guessed, the more likely does the answer match your needs.
Let me give an example:
I have these matrices
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], f=[4 6 3; 2 1 3; 9 4 2] .........
And an ideal output could be:
b==f
So you want to check if any of the two matrices are equal? What if there are more than 2 matrices that are equal?
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일

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일

0 개 추천

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

Is this a m file. Do i have to convert it to funktion?
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);
thanks luca

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2023년 3월 1일

편집:

2023년 3월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by