For loop the last end first values problem

9 次查看(过去 30 天)
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 个评论
Erkan
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
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
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 个评论
Erkan
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by