이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
For loop the last end first values problem
조회 수: 12 (최근 30일)
이전 댓글 표시
Erkan
2021년 8월 31일
Hi everybody, i have problem for loop. problem is that i dont take the last value of vector in the second loop to the first value of vector in the firts loop, below program code;
W=zeros(1,51);
A(1)=0;
for x=1:length(W)
for y=1:50
A(y+1)=A(y)+...........
end
W(x)=A(end); (when i make in this way, the all value of vector W is being the same)
end
댓글 수: 16
DGM
2021년 8월 31일
편집: DGM
2021년 8월 31일
A(y+1)=A(y)+...........
So we don't know if A is a function of anything or whether the last element ever changes. Is A defined anywhere prior? Is it longer than 51 elements?
W(x)=A(end);
In order to know how to fix it, we need to know what it's doing and what it's supposed to do.
Erkan
2021년 8월 31일
hello DGM, the 2nd for loop is running and calculating a function and I want to take the last calculated value and assign it to the first value of the vector W running the 1st loop. When the 2nd loop is completed, the last value of vector A is different from each other and I want to assign these different values to vector W. for example W(1)=5 and W(2)=36.
Erkan
2021년 8월 31일
i tried the while loop instead of the for loop in the firts loop;
x=1;
while x<=51
for y=1:50
end
x=x+1
end
but still there is the same problem
also i tried below form;
for x=1
for y=1:50
end
x=x+1
if x<=51
break; end
end
in this way, the value x become only 2 and dont increase more.
DGM
2021년 8월 31일
Without knowing how A is calculated or how long it is, I can only guess what the last value will be or whether it will ever change.
If I can assume that A is no longer than 51 elements and that whatever process that generates it will produce unique values, then Mathieu's answer is an example. If one or both of those assumptions is incorrect, then you'll have to elaborate. Otherwise, you are the only person who has access to your own code.
Erkan
2021년 8월 31일
what i want doing in program, Se2(x)=Se(end)-mean(Se), that is while x=1, second loop will work and for Se variable will be the 50 result. and then the last value of Se will subtract average Se value. for x=2 program will work again.
Mathieu NOE
2021년 9월 1일
hi
your code cannot be run
I see (at least) those constants not initialized :
N0 Aw Cw Ae Ce ug ue uw Ees Egs Ewl Kb
also T is not defined in lines :
tge(i)=(ug/ue)*teg(i)*exp((Ees-Egs)/(Kb*T));
tew(i)=(ue/uw)*twe(i)*exp((Ewl-Ees)/(Kb*T));
tgw(i)=(ug/uw)*twe(i)*exp((Ewl-Egs)/(Kb*T));
please check your code before posting it !!
DGM
2021년 9월 1일
I haven't been at the computer much today.
Looking at it, Se2 is being filled with NaN. If you put the following inside the innermost loop (at the end):
if isnan(Ne(i+1))
[j x i] % show the loop indices
return;
end
execution will stop once things start turning NaN. You can see that Se is NaN because p3,p4 are NaN (and p1 and p2 are approaching absurdly large values). These are NaN because Ne is NaN. Ne is NaN because the m# variables are NaN. The m# variables are NaN because the arguments to fNe() are approaching Inf. The operations within fNe() result in two of the terms being Inf. Inf-Inf is NaN. If you plot any of the partially populated vectors, you should be able to look at a subset of the vector and see the rapid growth.
I don't have a ready solution for this. I don't really have my head wrapped around what the code is doing mathematically. You'll have to figure out what this observation means in that context. You can make use of breakpoints or simple tests like that shown above to try to supervise the process and further troubleshoot. If possible, it may help to break the problem into parts to simplify testing.
Erkan
2021년 9월 1일
편집: Erkan
2021년 9월 1일
Hello DGM and Mathieu
Firstly, thanks you for interested in my problem. Actually the iteration numbers of the 2nd and 3rd loops are more high, 2nd loop: 1001 and 3rd loop:1000. i intently given small values to take short time while working your. yes, in the small iteration values heppen the NaN states but not more high iteration values. i edited my program and added.
Erkan
2021년 9월 3일
Hi again, i solved the problem in my program. Error is due to no any a connect between the 2nd and 3rd looop. i taken RN variable in program into the 2nd loop, problem is solved. i wanted to share so that for anyone become useful.
답변 (1개)
Mathieu NOE
2021년 8월 31일
hello
your code works - depends what you do in the first loop. here with rand to create different outputs for A(end)
you could have preallocated memory for A as well (as for W)
My results for W :
W =
Columns 1 through 5
25.9275 24.6355 28.5185 24.8521 24.2186
Columns 6 through 10
28.4241 24.2731 27.0010 26.0431 22.7694
Columns 11 through 15
24.4161 25.4517 25.1269 26.4411 25.9014
Columns 16 through 20
29.2451 25.3311 25.1996 24.2821 25.5079
Columns 21 through 25
26.7850 25.5720 24.6583 28.1824 23.9135
Columns 26 through 30
26.2461 24.0711 27.3557 25.0476 25.6504
Columns 31 through 35
24.8361 23.7875 26.3622 24.0471 26.9280
Columns 36 through 40
23.7483 21.7386 24.6370 25.7321 24.5716
Columns 41 through 45
26.3787 23.5214 26.7891 26.3458 27.8958
Columns 46 through 50
24.5838 24.4152 21.9655 26.6178 21.9926
Column 51
27.9023
Code :
clc
clearvars
W=zeros(1,51);
A=zeros(1,51);
for x=1:length(W)
for y=1:50
A(y+1)=A(y)+rand(1);
end
W(x)=A(end); % (when i make in this way, the all value of vector W is being the same)
end
댓글 수: 8
Mathieu NOE
2021년 8월 31일
hello
well, do you have a case where you know what you expect that the code is not generating ? for the time being I don't see where is the issue
Mathieu NOE
2021년 8월 31일
I understand , the rand is just for my demo to not generate always the same final value for A (A(end)) otherwise you will always have same W(x)
so if your function does generate a different A(end) for each run (this is to be checked by yourself) , there is no reason why W(x) would be always the same
Erkan
2021년 8월 31일
yes, in the program seem no any problem but i dont understand why the code is not working that as i want
Mathieu NOE
2021년 8월 31일
in debug mode , have you checked A(end) for several iterations ? does it actually change or not ?
Erkan
2021년 8월 31일
i tried for the more small iterations, yes , the value A(end) is changing in the each 2. loop
Mathieu NOE
2021년 8월 31일
so it should be the same for W(x)
can you display both A(end) and W(x) side by side for each iteration ?
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)