Create new matrices based on the number of unique values

3 ビュー (過去 30 日間)
Elysi Cochin
Elysi Cochin 2022 年 5 月 13 日
回答済み: Stephen23 2022 年 5 月 14 日
having a matrix
M = [4 7 2; 2 4 7; 2 2 4];
M =
4 7 2
2 4 7
2 2 4
based on the number of unique values (in this case 3), i want to create, 3 new matrices, and its value as follows, where all there is 2, in the first matrix, iall those places needs to be replaced with 1 and all the rest 0, same on taking the next unique number, replace all those places of 4 with 1 and the rest 0 in the second matrix and then next unique number
Then I need to create new matrices based on the number of unique values and replace the ones in the matrix with the values in M and find the sum of the matrix and find the greatest matrix of its sum.
M = [4 7 2; 2 4 7; 2 2 4];
sum_M1 = 8;
sum_M1 = 12;
sum_M1 = 14;
so M3 is greatest and i need to get that 3rd unique value, that is 7.

採用された回答

Stephen23
Stephen23 2022 年 5 月 14 日
Those three matrices are a red-herring.
M = [4,7,2;2,4,7;2,2,4]
M = 3×3
4 7 2 2 4 7 2 2 4
[U,~,X] = unique(M(:));
S = accumarray(X,M(:))
S = 3×1
8 12 14
[~,Y] = max(S);
U(Y)
ans = 7

その他の回答 (1 件)

Matt J
Matt J 2022 年 5 月 13 日
M = [4 7 2; 2 4 7; 2 2 4];
x=reshape(unique(M),1,1,[]);
output=(M==x)
output = 3×3×3 logical array
output(:,:,1) = 0 0 1 1 0 0 1 1 0 output(:,:,2) = 1 0 0 0 1 0 0 0 1 output(:,:,3) = 0 1 0 0 0 1 0 0 0

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by