You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
For loop the last end first values problem
11 views (last 30 days)
Show older comments
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 Comments
DGM
on 31 Aug 2021
Edited: DGM
on 31 Aug 2021
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
on 31 Aug 2021
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.
DGM
on 31 Aug 2021
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.
Mathieu NOE
on 1 Sep 2021
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
on 1 Sep 2021
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
on 1 Sep 2021
Edited: Erkan
on 1 Sep 2021
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.
Answers (1)
Mathieu NOE
on 31 Aug 2021
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 Comments
Mathieu NOE
on 31 Aug 2021
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
on 31 Aug 2021
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
Mathieu NOE
on 31 Aug 2021
in debug mode , have you checked A(end) for several iterations ? does it actually change or not ?
Mathieu NOE
on 31 Aug 2021
so it should be the same for W(x)
can you display both A(end) and W(x) side by side for each iteration ?
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)