How to output a cell array as a table where the arrays contain vectors of different length?
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
L'O.G.
am 6 Mai 2023
Bearbeitet: Atsushi Ueno
am 6 Mai 2023
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 Kommentare
Akzeptierte Antwort
Star Strider
am 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 Kommentare
Weitere Antworten (1)
Atsushi Ueno
am 6 Mai 2023
Bearbeitet: Atsushi Ueno
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!