For loop the last end first values problem

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

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.
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.
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.
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.
i understand, i can send your email adress the all code
or i can send message by the matlab
I don't answer questions via email. What happens on the forum stays on the forum.
Erkan
Erkan on 31 Aug 2021
Edited: Stephen23 on 31 Aug 2021
Hello DGM, below code
Hello, excuse me, i added all program, now
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.
Are there not having any a idea or recommend about this problem
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 !!
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
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.
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.

Sign in to comment.

Answers (1)

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

Hi Mathieu, thanks you answer but this code dont works that like i want
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
Erkan
Erkan on 31 Aug 2021
Edited: Erkan on 31 Aug 2021
The vector A is not become from the random numbers, it is results of the solution a function
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
yes, in the program seem no any problem but i dont understand why the code is not working that as i want
in debug mode , have you checked A(end) for several iterations ? does it actually change or not ?
i tried for the more small iterations, yes , the value A(end) is changing in the each 2. loop
so it should be the same for W(x)
can you display both A(end) and W(x) side by side for each iteration ?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Tags

Asked:

on 31 Aug 2021

Commented:

on 3 Sep 2021

Community Treasure Hunt

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

Start Hunting!