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일

0 개 추천

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일

0 개 추천

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

The MATLAB approach:
X = 1:1000
X = 1×1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Y = X.^2 + X + 1
Y = 1×1000
3 7 13 21 31 43 57 73 91 111 133 157 183 211 241 273 307 343 381 421 463 507 553 601 651 703 757 813 871 931
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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:

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 12월 10일

댓글:

2024년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by