Concatenate arrays within a loop,
93 次查看(过去 30 天)
显示 更早的评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!