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

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'

 채택된 답변

Adam Danz
Adam Danz 2018년 7월 13일
편집: Adam Danz 2018년 7월 13일
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

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
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?
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 ?
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.
I have tried : format long , format short , double(sumOfValues) and the output still fraction number :(
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.
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?
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)
"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:
Thank you so much both of you.
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
@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

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2018년 7월 13일

댓글:

2023년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by