Concatenate matrices from cell array

19 ビュー (過去 30 日間)
Jasper
Jasper 2014 年 11 月 11 日
移動済み: Dyuman Joshi 2024 年 1 月 31 日
Hello,
I have a cell array with different length matrices in each cell, i.e. the number of columns is always the same, but the number of rows differs. I want to combine all those matrices together in 1 big matrix, and have difficulties doing so. I know it's best to pre-allocate the final matrix before filling it, so thats what I do, but I can't get the actual filling to work.
For example:
A = rand(5,2);
B = rand(7,2);
C = rand(15,2); % just 3 diff length matrices
cell = {A B C}; % ABC into cell array
nrows = cellfun(@length, combi); %get rows in each cell
sum_rows = sum(nrows);% get total rows required
Final = zeros(sum_rows,2); % pre-allocate matrix of appropriate size
Please help me to formulate a loop (over n cells in cell array) which adds the nth cell to the Final matrix each iteration. Thanks very much for any help.
Jasper

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 11 月 11 日
A = rand(5,2);
B = rand(7,2);
C = rand(15,2);
cc = {A B C};
out = cat(1,cc{:});
  1 件のコメント
Subhosit Ray
Subhosit Ray 2018 年 6 月 1 日
移動済み: Dyuman Joshi 2024 年 1 月 31 日
you make things look so simple =) good job andrei

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by