Concatenate arrays within a loop,

22 ビュー (過去 30 日間)
VBBV
VBBV 2018 年 6 月 30 日
編集済み: Stephen23 2018 年 6 月 30 日
I would like to concatenate arrays inside a loop.. how can I do it ?

採用された回答

Stephen23
Stephen23 2018 年 6 月 30 日
編集済み: Stephen23 2018 年 6 月 30 日
Easy, given any number of arrays in a cell array C. You could use a loop like this, although it is inefficient because the output array is not preallocated:
C = {[1,2],[3,4],...} % cell array of arrays.
A = C{1};
for k = 2:numel(C)
A = [A;C{k}];
end
Simpler and more efficient would be to concatenate them all at once, without a loop, e.g.:
A = vertcat(C{:})
Whatever you do, do NOT try to access variable names dynamically in a loop! Read this to know why:
Using an array (e.g. a cell array) and indexing is much simpler, neater, much more efficient, less buggy, and easier to debug.

その他の回答 (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