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'
채택된 답변
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에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
