Cell two mat Execution
Show older comments
A cell array with size 1*178 inside each cell column vector of different sizes. I have to convert this to the matrix with 178 columns and the corresponding column vectors.
Any help is apppreciated! Thanks!
Accepted Answer
More Answers (1)
Image Analyst
on 26 Jan 2016
Here's one way that's fairly intuitive and easy to follow.
% Create a cell array, ca, that is random sample data.
for k = 1 : 178
% Get a random length
randomLength = randi(1000, 1);
% Make a column vector of random length.
thisData = rand( randomLength, 1);
% Put this randomly sized data into the cell.
ca{k} = thisData;
end
% =========== MAIN CODE =============================
% Examine the cell array to find the longest vector.
% I'm sure there's a more compact and cryptic cellfun() way,
% but here's an easy-to-follow "for loop" way to find the tallest column vector.
for k = 1 : length(ca);
height(k) = length(ca{k});
end
maxHeight = max(height)
% Now make a 2D numerical array that we can stuff the vectors into
array2d = zeros(maxHeight, length(ca)); % Preallocate.
for k = 1 : length(ca);
thisColumnsHeight = length(ca{k});
array2d(1:thisColumnsHeight, k) = ca{k};
end
% array2d is the output. The bottom rows of any columns will be 0 if
% that column didn't have maxHeight elements in it.
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!