How to output a cell array as a table where the arrays contain vectors of different length?
21 views (last 30 days)
Show older comments
I have a cell array C that is 3 x 10, with each element containing a vector of numbers of type double. Each vector is of a different length. How do I make a table where the first column is C{1,1}, the second column is C{1,2}, and so on?
0 Comments
Accepted Answer
Star Strider
on 6 May 2023
V1 = randn(5,1)
V2 = randn(10,1)
C = {V1 V2}
T = cell2table(C)
T{:,1}{:}
T{:,2}{:}
The iundividual variables remain cell arrays, so there is no problem with their not having the same internal dimensions.
.
0 Comments
More Answers (1)
Atsushi Ueno
on 6 May 2023
Edited: Atsushi Ueno
on 6 May 2023
C = cell(3,10); % cell array C that is 3 x 10 with each element containing a vector of numbers of type double.
C = cellfun(@(x) rand(randi(10),1),C,'uni',false) % Each vector is of a different length. This is sample data.
C = reshape(C',1,[]); % reshape the cell array from 3 x 10 to 1 x 30 (row priority)
max_len = max(cellfun(@length,C)); % get the longest vector length
for k = 1:size(C,2)
C{k} = [C{k}; NaN(max_len-size(C{k},1),1)]; % fill each vector with missing to have the longest vector length
end
M = cell2mat(C);
T = array2table(M) % How do I make a table where the first column is C{1,1}, the second column is C{1,2}, and so on?
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!