How to use for loop for iterations.

126 次查看(过去 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 个)

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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by