Store all iteration loop outputs in a matrix

clc;
a = 5;
b = 20;
for i = 1:10
j = (i + 25);
k = (j*b);
m =[i j k];
end
********************************
output:
m =
10 35 700
******************************************************************
Hi,
I am writing a program as mentioned above to store all iteration loop outputs in a matrix.
However, the output just displays the last iteration.
Could you please let me know the right code which is able to display all the iterations from 1 to 10.
Thank you very much

 採用された回答

Mischa Kim
Mischa Kim 2014 年 7 月 8 日
編集済み: Mischa Kim 2014 年 7 月 8 日

16 投票

UTS, use instead
m(i,:) = [i j k];
The output is an array m with 10 rows, 3 columns.

9 件のコメント

UTS
UTS 2014 年 7 月 8 日
編集済み: UTS 2014 年 7 月 8 日
Your prompt response is greatly appreciated,
I tried but i got this error:
Subscripted assignment dimension mismatch.
Error in Untitled3 (line 7)
m(i,:) = [i j k];
Mischa Kim
Mischa Kim 2014 年 7 月 8 日
Strange. Copy-paste
clc;
a = 5;
b = 20;
for i = 1:10
j = (i + 25);
k = (j*b);
m(i,:) = [i j k];
end
into the command window and execute.
UTS
UTS 2014 年 7 月 8 日
Thank you very much
but still getting an error message
Subscripted assignment dimension mismatch.
UTS
UTS 2014 年 7 月 8 日
m =
1 26 520
2 27 540
3 28 560
4 29 580
5 30 600
6 31 620
7 32 640
8 33 660
9 34 680
10 35 700
***********************************************
Finally got the answer.
Thank you very much for your time and assistance,
Mischa Kim
Mischa Kim 2014 年 7 月 8 日
What was the issue?
Naeem Ullah
Naeem Ullah 2017 年 4 月 14 日
Mischa Kim, Sir, you got good skills in Matlab. I was facing a problem like that in Matlab code, I apply this it really works. Thank you very much.
David Horowitz
David Horowitz 2017 年 10 月 5 日
Exactly what I was looking for as well. Thanks for posting this answer!
Durga Aryal
Durga Aryal 2018 年 2 月 2 日
How did you solve the problem of subscripted dimension mismatch?
Ashish Kundaliya
Ashish Kundaliya 2019 年 4 月 5 日
Hey @durga aryal, you might be doing a mistake like if you haven't applied for loop then it will show exactly what you are asking for?!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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