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)
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?

Réponse acceptée

Star Strider
Star Strider le 6 Mai 2023
The cell2table function is an option —
V1 = randn(5,1)
V1 = 5×1
0.0917 -0.2251 2.6081 -0.5140 0.2548
V2 = randn(10,1)
V2 = 10×1
2.2572 -0.5459 1.1909 0.1441 0.9385 -0.8397 -1.2509 0.7773 -1.0841 0.6843
C = {V1 V2}
C = 1×2 cell array
{5×1 double} {10×1 double}
T = cell2table(C)
T = 1×2 table
C1 C2 ____________ _____________ {5×1 double} {10×1 double}
T{:,1}{:}
ans = 5×1
0.0917 -0.2251 2.6081 -0.5140 0.2548
T{:,2}{:}
ans = 10×1
2.2572 -0.5459 1.1909 0.1441 0.9385 -0.8397 -1.2509 0.7773 -1.0841 0.6843
The iundividual variables remain cell arrays, so there is no problem with their not having the same internal dimensions.
.

Plus de réponses (1)

Atsushi Ueno
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 = 3×10 cell array
{8×1 double} { 6×1 double} { 9×1 double} {7×1 double} {10×1 double} {6×1 double} {[ 0.4365]} {8×1 double} {7×1 double} {10×1 double} {4×1 double} { 3×1 double} {10×1 double} {5×1 double} { 9×1 double} {4×1 double} {7×1 double} {[ 0.1047]} {8×1 double} { 6×1 double} {2×1 double} {10×1 double} { 3×1 double} {8×1 double} { 7×1 double} {9×1 double} {2×1 double} {7×1 double} {4×1 double} {10×1 double}
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?
T = 10×30 table
M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12 M13 M14 M15 M16 M17 M18 M19 M20 M21 M22 M23 M24 M25 M26 M27 M28 M29 M30 ________ _______ _______ _______ ________ _______ _______ ________ _______ _______ ________ _______ ________ _______ _______ ________ __________ _______ ________ _______ _______ _______ _______ _________ ________ ________ _______ ________ ________ ________ 0.67795 0.54259 0.50273 0.14941 0.57303 0.9158 0.43649 0.35172 0.80154 0.21581 0.57813 0.50141 0.6775 0.61059 0.71441 0.11474 0.00046646 0.10471 0.8915 0.21815 0.23308 0.34391 0.2963 0.44596 0.8661 0.17985 0.33507 0.42969 0.83292 0.037335 0.26072 0.82396 0.47925 0.43309 0.97114 0.88912 NaN 0.80326 0.76776 0.27784 0.057763 0.65258 0.92312 0.99338 0.66967 0.62937 0.05594 NaN 0.29596 0.41348 0.2854 0.36016 0.25524 0.37373 0.33938 0.25718 0.11697 0.37942 0.80523 0.20456 0.099828 0.92535 0.4002 0.88799 0.071483 0.9386 NaN 0.078987 0.46178 0.69999 0.32551 0.84009 0.91824 0.43338 0.25788 0.060773 0.082633 NaN 0.92806 0.8242 NaN 0.40213 0.73348 0.73346 0.037813 0.5607 NaN 0.53054 0.034572 0.76031 0.23965 0.49722 0.32954 0.51387 0.03989 0.19626 NaN 0.54193 0.7128 0.65233 0.86191 NaN 0.95612 0.82767 0.15016 0.85778 0.84131 NaN 0.9225 0.20252 NaN 0.77126 NaN 0.025001 0.37315 0.56917 NaN 0.057453 0.48707 0.15782 0.32705 0.98535 0.77319 0.14784 0.16337 0.10886 NaN 0.26083 0.22961 0.81654 NaN NaN 0.1453 0.67902 0.83244 NaN 0.58346 NaN 0.30779 0.91652 NaN 0.941 NaN 0.17162 0.61906 0.51736 NaN 0.47657 NaN 0.92609 0.47499 0.51679 0.4195 0.26425 0.413 0.22741 NaN 0.067544 0.87654 0.58669 NaN NaN 0.056195 NaN 0.433 NaN 0.95777 NaN 0.012506 0.54986 NaN 0.34981 NaN 0.5646 0.4438 0.029692 NaN 0.035797 NaN 0.11212 0.28585 NaN 0.27566 0.37442 0.26519 NaN NaN 0.65032 0.31668 0.29703 NaN NaN 0.17516 NaN 0.86174 NaN 0.11558 NaN 0.72114 NaN NaN 0.54326 NaN 0.0080328 0.78185 0.26626 NaN 0.16669 NaN 0.37189 0.98941 NaN 0.12545 NaN 0.78176 NaN NaN 0.87475 NaN 0.749 NaN NaN 0.47969 NaN 0.39805 NaN NaN NaN 0.82336 NaN NaN 0.79291 NaN 0.38775 NaN 0.4328 NaN NaN NaN 0.36659 NaN NaN 0.53446 NaN 0.22659 NaN NaN NaN NaN 0.57487 NaN NaN 0.916 NaN 0.87254 NaN NaN NaN NaN NaN NaN 0.37603 NaN NaN NaN 0.774 NaN NaN NaN 0.69011 NaN NaN NaN NaN 0.41873 NaN NaN NaN NaN 0.62737 NaN NaN 0.99221 NaN NaN NaN NaN NaN NaN NaN NaN 0.55637 NaN NaN NaN NaN NaN NaN NaN 0.84201

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by