フィルターのクリア

Error using cell/unique (line 85) Cell array input must be a cell array of character vectors.

10 ビュー (過去 30 日間)
Farzana Kousar
Farzana Kousar 2020 年 3 月 18 日
回答済み: BhaTTa 2024 年 6 月 11 日
i have created a cell to store [4x4] matrices. total matrices in a cell are 10. i want to find unique matrices of that cell and their occurrence.???
how can i do that in matlab. unique wont woks with a cell having matrix entries.

回答 (1 件)

BhaTTa
BhaTTa 2024 年 6 月 11 日
To find unique matrices within a cell array and count their occurrences in MATLAB you can do it by converting each matrix to a string or a canonical form that can be compared, then using unique on these representations to find duplicates and count occurrences. Here's a step-by-step approach:
Step 1: Convert Matrices to Strings
One way to compare matrices is to convert them into strings. You can use the mat2str function for this purpose.
% Example cell array of matrices
cellOfMatrices = {
[1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16],
[1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16], % Duplicate
[4, 3, 2, 1; 8, 7, 6, 5; 12, 11, 10, 9; 16, 15, 14, 13],
randi(100, 4, 4), % Random matrices
randi(100, 4, 4),
[4, 3, 2, 1; 8, 7, 6, 5; 12, 11, 10, 9; 16, 15, 14, 13], % Duplicate
randi(100, 4, 4),
randi(100, 4, 4),
randi(100, 4, 4),
randi(100, 4, 4)
};
% Convert each matrix in the cell to a string representation
stringMatrices = cellfun(@mat2str, cellOfMatrices, 'UniformOutput', false);
Step 2: Find Unique Strings and Their Occurrences
Once you have the string representations, you can use the unique function along with the hist or accumarray functions to find unique matrices and their occurrences.
[uniqueStr, ~, ic] = unique(stringMatrices);
occurrences = accumarray(ic, 1);
% Display results
for i = 1:length(uniqueStr)
fprintf('Matrix:\n');
disp(uniqueStr{i});
fprintf('Occurrences: %d\n\n', occurrences(i));
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by