How to save the values after each iteration in a matrix ?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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 =
'787226.437500'
sumOfValues1 =
'162571.843750'
sumOfValues1 =
'257137.468750'
sumOfValues1 =
'366862.625000'
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;
end
12 Comments
Reema Alhassan
on 13 Jul 2018
I got this error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in untitled3 (line 242) a(i) = sumOfValues1
Adam Danz
on 13 Jul 2018
In your initial example, the value of 'sumOfValues1' was always 1 number. My sample code should work if this is the case.
What is the size of 'sumOfValues1'? Is the size the same on every iteration?
Also, did you read through the documentation I provided in the link?
Reema Alhassan
on 13 Jul 2018
because I have made this before
sumOfValues1=sprintf('%f', sumOfValues1);
when I remove it the error gone but I need this statement because I need the number to be in decimal format do you know how to do this ?
Adam Danz
on 13 Jul 2018
The output to sprintf() is not in decimal format. It's a string. Let me give you an example.
pi % decimal format 3.1416
sprintf('%f', pi) % string format '3.141593' (in quotes)
% If you'd like to see more decimal places
format long
pi % decimal format 3.141592653589793
The "format long" just displays more decimal places. In reality, even when using "format short", there are still lots of decimal places that matlab uses but doesn't display.
So just get rid of the sprintf() line unless you need the string for some other reason.
Reema Alhassan
on 13 Jul 2018
I have tried : format long , format short , double(sumOfValues) and the output still fraction number :(
Adam Danz
on 13 Jul 2018
I don't know what 'fraction number' is. When you take out the sprintf() line, you said it works. So, just take that line out. sprint() produces a string and strings are not numbers. I don't understand why you think you need the sprintf() line in the first place.
If you're still lost, attach the entire loop and some data I can run through it, please.
Reema Alhassan
on 13 Jul 2018
Fraction number is for example : 1.234565e+05 I used sprintf() before to make it in this format for example : 1.323555864 because These commands format long , format short and double(value) in my code don’t change the format and i don’t know why maybe i have to install some toolboxes?
Adam Danz
on 13 Jul 2018
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 =
logical
1
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)
Stephen23
on 13 Jul 2018
"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:
Reema Alhassan
on 14 Jul 2018
Thank you so much both of you.
Priteesh Ranjan
on 25 May 2023
How to store values if
for i = 1:0.01:1
its giving error Array indices must be positive integers or logical values which is very obvious as indices now wont be integral
Stephen23
on 25 May 2023
@Priteesh Ranjan: in general with MATLAB it is easier to loop over indices rather than over data values:
V = 1:0.01:1; % data
for k = 1:numel(V) % indices
V(k)
..
end
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)