For loop the last end first values problem
Mostra commenti meno recenti
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 Commenti
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
il 31 Ago 2021
Erkan
il 31 Ago 2021
DGM
il 31 Ago 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.
Erkan
il 31 Ago 2021
Erkan
il 31 Ago 2021
DGM
il 31 Ago 2021
I don't answer questions via email. What happens on the forum stays on the forum.
Erkan
il 31 Ago 2021
Erkan
il 31 Ago 2021
Erkan
il 31 Ago 2021
Erkan
il 31 Ago 2021
Mathieu NOE
il 1 Set 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
il 1 Set 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
il 3 Set 2021
Risposte (1)
Mathieu NOE
il 31 Ago 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 Commenti
Erkan
il 31 Ago 2021
Mathieu NOE
il 31 Ago 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
il 31 Ago 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
Erkan
il 31 Ago 2021
Mathieu NOE
il 31 Ago 2021
in debug mode , have you checked A(end) for several iterations ? does it actually change or not ?
Erkan
il 31 Ago 2021
Mathieu NOE
il 31 Ago 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 ?
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!