How to use for loop for iterations.

129 ビュー (過去 30 日間)
Zahid Iqbal Rana
Zahid Iqbal Rana 2014 年 12 月 10 日
コメント済み: Stephen23 2024 年 9 月 30 日
for i=1:3
a=rand(3,1);
b=5.*(a.^2);
c=[a b]
end
At the end i get a value of c that is of the order of 3x2, that is the final value that comes after3rd iteration. So value of c gets changed in each iteration. How I can get all value of c of the loop. In other words i want to get c of the order 9x2 (3 iterations and three values for each iteration).

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 10 日
編集済み: Azzi Abdelmalek 2014 年 12 月 10 日
c=[];
for i=1:3
a=rand(3,1);
b=5.*(a.^2);
c=[c;a b]
end
%or
c=zeros(9,2);
ii=1
for i=1:3
a=rand(3,1);
b=5.*(a.^2);
n=size(a,1);
c(ii:ii+n-1,:)=[a b]
ii=ii+3;
end
  1 件のコメント
Zahid Iqbal Rana
Zahid Iqbal Rana 2014 年 12 月 10 日
編集済み: Zahid Iqbal Rana 2014 年 12 月 10 日
You are Boss. Awesome

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

その他の回答 (1 件)

Edgar
Edgar 2024 年 9 月 29 日
I need to create a loop that will yiels 1000 outputs (from second 1 to second 1000), the output is an angle, and angle has its formula. Or to put it in simple terms, let's say, I've got an function f(x) = x^2 + x + 1 and let's say I needed 1000 outputs when my x_initial = 1 and x_final = 1000. Thank you in advance.
  3 件のコメント
Edgar
Edgar 2024 年 9 月 29 日
Thank you. Could you please clarify the second line of the code, the x = 1x1000, Is that an "x", or asterisk? I am quite a novice using MATLAB. I've never seen that symbol and how do you type it?
Thank you again
Stephen23
Stephen23 2024 年 9 月 30 日
"Could you please clarify the second line of the code..."
This is the second line of code:
Y = X.^2 + X + 1
It refers to the vector X defined on the first line of code, and uses the following operators:
"...the x = 1x1000, Is that an "x", or asterisk?"
That is not the second line of code. That is how MATLAB displays the size of the result of the second line of code. MATLAB often displays the size and class automatically if you do not suppress the display with a semicolon.
But, because you asked, that Unicode character is officially called "Multiplication Sign":
"I am quite a novice using MATLAB. I've never seen that symbol and how do you type it?"
The multiplication sign is commonly used in mathematics to indicate multiplication, thus its name.
I doubt that many keyboards support typing it. In the unlikely event that you need to type it then use the Windows Character Map or MacOS Character Viewer or similar. Or use LaTeX or some other markup language.
But given your confusion between what is a MATLAB command and what is displayed result, this would likely be a much better use of your time:

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

カテゴリ

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