how to add colmun to vector
4 ビュー (過去 30 日間)
古いコメントを表示
hello how can i add to my colmn vector using for loop ?
for exmaple i have this vector [0;0] and i want it to grow by one and keep adating like this for exmaple [0 1;0 1] and then [0 1 2; 0 1 2] and then [0 1 2 3; 0 1 2 3 ] and so on
1 件のコメント
Guillaume
2018 年 1 月 4 日
Note that growing arrays in a loop is not recommended. It adversely affects performance. Preallocation and indexing is recommended instead.
回答 (3 件)
tomer polsky
2018 年 1 月 4 日
編集済み: Guillaume
2018 年 1 月 4 日
1 件のコメント
Guillaume
2018 年 1 月 4 日
Torsten's way may be too complicated but it certainly performs better than the above. In particular, in the first step of the loop, the above replaces
x = [0;0]
by
x = [1;1]
The next steps of the loop do indeed grow x (not recommended) so the end result is:
x = [1 2 3 4 5; 1 2 3 4 5]
not
x = [0 1 2 3 4;0 1 2 3 4] %or maybe [0 1 2 3 4 5;0 1 2 3 4 5]
as was requested
Guillaume
2018 年 1 月 4 日
numsteps = 5; %and not using hardcoded ends for loops
x = zeros(2, numsteps);
for i = 1:numsteps
x(:, i) = i-1;
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!