MATLAB Answers


How to save the values after each iteration in a matrix ?

Asked by Reema Alhassan on 13 Jul 2018
Latest activity Commented on by Reema Alhassan on 14 Jul 2018
Hello everybody ,
I did some calculation in a loop and this is the output I need to put the value after each iteration in Nx1 matrix how can I do this?
sumOfValues1 =
sumOfValues1 =
sumOfValues1 =
sumOfValues1 =


Sign in to comment.

1 Answer

Answer by Adam Danz
on 13 Jul 2018
Edited by Adam Danz
on 13 Jul 2018
 Accepted Answer

Follow this example for n=6 loops where I store your sumOfValues1 in a n-by-1 vector. Also, take some time to read through this document so you understand how indexing works.
% initialize variable
n = 6;
a = zeros(n,1);
% Loop through and store result
for i = 1:n
a(i) = sumOfValues1;


Reema, I think you're not understanding something that's critical to understand.
1.2e+3 is the same as 1200.
It's the same exact number and matlab treats both forms exactly the same.
  • 1.2e+2 is the same as 120.
  • 1.2e+4 is the same as 12000.
  • 1.2e+5 is the same as 120000.
  • 1.2e+6 is the same as 1200000.
  • 1.23456789e+6 is the same as 1234567.89
You can even test that in matlab
1.23456789e+6 == 1234567.89
ans =
This is exactly why your code WORKS when you remove the sprintf() line.
To further convince you,
a = 1.23456789e+6; %1234567.89;
round(a) %1234568 (rounded)
"1.234565e+05 I used sprintf() before to make it in this format..."
Then you have a char vector or a string, not a numeric. You need to learn about the basic data types:

Sign in to comment.