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

2 views (last 30 days)
Nasim Nobary on 2 Jun 2022
Edited: Jon on 2 Jun 2022 Jon on 2 Jun 2022
Edited: Jon on 2 Jun 2022
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