How to quickly save a matrix by adding rows?

3 ビュー (過去 30 日間)
Benson Gou
Benson Gou 2021 年 6 月 22 日
コメント済み: Stephen23 2021 年 6 月 22 日
Dear All,
I calculate a row Ai and save it in my defined matrix A using the following way:
A = [A; Ai];
But I foud it took a lot of time. Is there a faster way?
Thanks a lot.
Benson

採用された回答

dpb
dpb 2021 年 6 月 22 日
Yes. Preallocate the array and index into it...without context it's conjecture what you have/are doing, but the general idea is
A=zeros(max(variable_i),size(A,2)); % preallocate A as array of zeros
for i=1:max(variable_i) % for the loop size each i
% whatever to compute Ai
...
A(i,:) = Ai; % store the new row in A
end
The above will not show degradation in speed as i increases.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by