MATLAB Answers

For loop the last end first values problem

조회 수: 2(최근 30일)
Erkan
Erkan 2021년 8월 31일
댓글: Erkan 2021년 9월 3일
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월 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
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 ?

댓글을 달려면 로그인하십시오.

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by