How to output a cell array as a table where the arrays contain vectors of different length?
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Star Strider
le 6 Mai 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 commentaires
Plus de réponses (1)
Atsushi Ueno
le 6 Mai 2023
Modifié(e) : Atsushi Ueno
le 6 Mai 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 commentaires
Voir également
Catégories
En savoir plus sur Cell Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!