フィルターのクリア

how to increase the rows of an array in each loop?

10 ビュー (過去 30 日間)
saharsahar
saharsahar 2012 年 2 月 25 日
回答済み: Dmitry Mittov 2015 年 3 月 13 日
Dear all, How to increase the elemnts of a multi dimentional array in each loop ? for example at first A=[], the next loop it will be A=[2 3 5 7] the third A= [ 2 3 5 7; 3 5 3 1 ] and so on. the colomn of A is attributes which is application-dependent.(I preferably want to use : to show all colomns)
Thanks

回答 (3 件)

Jan
Jan 2012 年 2 月 25 日
A = [];
for i = 1:5
A = cat(1, A, rand(1, 4));
% or:
% A = [A; rand(1, 4)];
end
But consider, that this is not efficient, because Matlab has to reserve the memory for the matrix in each iteration again and copy the former values. Look for "pre-allocation" in this forum to see better methods.

Dmitry Mittov
Dmitry Mittov 2015 年 3 月 13 日
sz = [5 4];
cell_A = arrayfun(@(i) rand(sz(2),1), 1:sz(1), 'UniformOutput', false);
A = cell2mat(cell_A)';
In my case I had vector X and wanted to get the matrix of [X; X .^ 2; X .^3; ... X .^ n]'. Not sure that works effective (in case of memory consumption), but looks so functional.

saharsahar
saharsahar 2012 年 2 月 26 日
Thanks Jan, That is great . However I need to have another loop and I want "A" will be different for each loop. I mean, for example there are 6 object (firs loop) which each object has its own "A" matrix in following code I define the Loop j. In this case it gives me problem.
A = [];
for j=1:6
for i = 1:5
A(j) = cat(1, A(j), rand(1, 4));
% or:
% A = [A; rand(1, 4)];
end
end
can you help me please in this specific case? Thanks
  4 件のコメント
saharsahar
saharsahar 2012 年 2 月 26 日
The colomns of A is actually application dependent. it will be a variable based on the features of inputs. in this example I assume it will be 4 . the rows of A also will be increased in each loop based on a "if condition". so I don know exactly how many rows will be, as it depends to accept if conditin or not. so here i just showing a simple example of A with 4 cols and 5 rows.
However I have 6 Objects which each of them have a A matrix.(See the code plz)
Is that make sense?
Thanks
saharsahar
saharsahar 2012 年 2 月 26 日
I got My answers.Tnx

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

カテゴリ

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