Combine Array from different cell

20 ビュー (過去 30 日間)
Amirul Azman
Amirul Azman 2019 年 3 月 18 日
コメント済み: jenifer Ask 2019 年 12 月 28 日
Hello, i am still new to Matlab and coding. Currently im stuck in how to combine these array from different cells into one cell.
Each cell array has different number of rows!
Can I combine alltogether into one cell. If yes, is there any efficient way to do this?
Thank you in Advance.
Sketch.png

回答 (2 件)

madhan ravi
madhan ravi 2019 年 3 月 18 日
編集済み: madhan ravi 2019 年 3 月 18 日
{cat(1,yourcell{:})}
%or
{vertcat(yourcell{:})}

Jos (10584)
Jos (10584) 2019 年 3 月 18 日
This is called concatenation. See the documentation of the function CAT. Matlab allows you to concatenate a bunch of column vectors into a single column (or row vectors into a single row), even if they have different lengths.
v1 = [1 ; 2 ; 3] ;
v2 = [5 ; 6] ;
Y = cat(1, v1, v2) % a single column vector
However, concatenating column vectors if different lengths into multiple columns is not allowed, because the resulting array is not rectangular. Somehow you need to fill up the empty spaces. This is exactly what my function PADCAT does:
% cat(2,v1,v2) % error!
M = padcat(v1, v2)
When your vectors are stored in a cell array, as in your question, using comma-separated list expansion makes it quite easy.
C = { [1 ; 2 ; 3], 4, [5 ; 6 ; 7 ; 8 ; 9], [10 ; 11] }
M = padcat(C{:})
PADCAT can be downloaded from the File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/22909-padcat

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by