How to output a cell array as a table where the arrays contain vectors of different length?
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2023-5-6
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 个评论
更多回答(1 个)
Atsushi Ueno
2023-5-6
编辑:Atsushi Ueno
2023-5-6
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!