Finding two similar Matrix in a whole set
이전 댓글 표시
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
2023년 3월 1일
do you mean equal when you say similar?
Muhammed Ali
2023년 3월 1일
Dyuman Joshi
2023년 3월 1일
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.
Sufiyan
2023년 3월 1일
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
2023년 3월 1일
isn't this pretty much the same as my solution?
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?
Muhammed Ali
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.
Muhammed Ali
2023년 3월 1일
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
2023년 3월 1일
채택된 답변
추가 답변 (2개)
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
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
Muhammed Ali
2023년 3월 1일
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);
Muhammed Ali
2023년 3월 1일
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!