How to use for loop for iterations.

131 vues (au cours des 30 derniers jours)
Zahid Iqbal Rana
Zahid Iqbal Rana le 10 Déc 2014
Commenté : Stephen23 le 30 Sep 2024
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).

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 10 Déc 2014
Modifié(e) : Azzi Abdelmalek le 10 Déc 2014
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 commentaire
Zahid Iqbal Rana
Zahid Iqbal Rana le 10 Déc 2014
Modifié(e) : Zahid Iqbal Rana le 10 Déc 2014
You are Boss. Awesome

Connectez-vous pour commenter.

Plus de réponses (1)

Edgar
Edgar le 29 Sep 2024
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 commentaires
Edgar
Edgar le 29 Sep 2024
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 le 30 Sep 2024
"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:

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by