I have a 32*1 cell array. How I can convert it to categorical?

6 ビュー (過去 30 日間)
Nasim Nobary
Nasim Nobary 2022 年 6 月 2 日
編集済み: Jon 2022 年 6 月 2 日

回答 (1 件)

Jon
Jon 2022 年 6 月 2 日
編集済み: Jon 2022 年 6 月 2 日
You could do something like this:
% As an example make a cell array whose elements are random matrices
numMatrices = 10; % number of matrices in cell array
matrixSize = 3; % size of square matrices
A = cell(numMatrices,1); % preallocate
for k = 1:numMatrices
A{k} = randn(matrixSize);
end
% Obtain a single scalar value to characterize each matrix
% ( for example I took largest element, but you need to adapt for your
% problem)
v = cellfun(@(X) max(abs(X(:))),A);
% turn it into a categorical vector
binEdges = [0,1,2,3];
categories = {'small','medium','large'}
categories = 1×3 cell array
{'small'} {'medium'} {'large'}
Ac = discretize(v,binEdges,"categorical",categories)
Ac = 10×1 categorical array
medium medium medium large medium large small large large medium
Note, I am assuming that you are going to provide a function that turns each matrix in your cell array to a double (non-integer) value, that is why I used the function discretize to bin them. If your function turns each matrix in your cell to an integer you could use the function categorical instead

カテゴリ

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