How to loop over a range of values?

305 ビュー (過去 30 日間)
Armando MAROZZI
Armando MAROZZI 2021 年 3 月 22 日
コメント済み: Armando MAROZZI 2021 年 3 月 22 日
I want to store some result from a function I built. These results are 7x1 for n iterations. How can I store values for 1:7, 8:14, 15:21,....?
Let me take an example:
% Let y be the outpuy of a model where y is 7x1 vector that is iterated n
% times. Therefore, the loop below is part of a bigger loop
x = nan(1008, 1); % store total results
for r = 1:size(x,1)/7
x(?,1) = y;
end
% basically, I need to store the first y result in the 1:7 rows of x, the
% second y result in the 8:14 rows of x, etc.
How can I do it?
Thanks!

採用された回答

David Hill
David Hill 2021 年 3 月 22 日
編集済み: David Hill 2021 年 3 月 22 日
Why not store in a matrix?
x = nan(n, 7);
for r = 1:n
x(n,:) = y';
end
If not you could just:
x = nan(7*n, 1);
for r = 1:n
x(7*(r-1)+1:7*r) = y;
end
  1 件のコメント
Armando MAROZZI
Armando MAROZZI 2021 年 3 月 22 日
thanks a lot! Very helpful!

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

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