How do I properly use the "while", "for", "loop" command in this problem?

조회 수: 1 (최근 30일)
Uche
Uche 2023년 3월 5일
편집: Voss 2023년 3월 8일
Good evening,
I am trying to write a code to determine the time required to complete an operation. Here's the scenario:
I have a column vector (A) = [71.213; 74.499; 79.175; 54.163; 83.008; 52.615]. For this vector, I want to take the first element, and substract a random number between (5,10) from it, until the element gets to zero then the code proceeds to the next element.
However, each time I substract a random number from an element, I want to generate a random number between (2,4), and save (add) to a variable, time (t), and keep adding successively generated random number (time) to this variable (t), until the end of the column vector.
The final displayed result will be time (t). Here's is my code, but it runs non-stop.
A = T(:,1); %Column vector
B = length(A); %size of column (number of elements)
%H = A(i); %Variable to store remaining harvest, after subtracting
t = 0; %Variable to store harvest time
%T = randi([2 4]); % time to pick fruit
%W = randi ([5 10]) %Weight of fruit
for i = 1:B;
H = A(i);
while H<=A(i);
H = A(i)- randi ([5 10]);
t = t + randi([2 4]);
if H>=A(i);
end
disp(t);
end
end
disp(t)
  댓글 수: 1
VBBV
VBBV 2023년 3월 6일
편집: VBBV 2023년 3월 7일
One way you can modify the code is below
A = [71.213; 74.499; 79.175; 54.163; 83.008; 52.615];%Column vector
B = length(A); %size of column (number of elements)
%H = A(i); %Variable to store remaining harvest, after subtracting
t = 0; %Variable to store harvest time
%T = randi([2 4]); % time to pick fruit
%W = randi ([5 10]) %Weight of fruit
for i = 1:B;
H = A(i);
while H<=A(i);
H = H- randi ([5 10]); % this is causing to run loop non stop
t = t + randi([2 4]);
if H<=0;disp(t); break;end
end
end
28 57 85 109 140 165

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

채택된 답변

Torsten
Torsten 2023년 3월 5일
편집: Torsten 2023년 3월 5일
A = [71.213; 74.499; 79.175; 54.163; 83.008; 52.615]; %Column vector
B = length(A); %size of column (number of elements)
%H = A(i); %Variable to store remaining harvest, after subtracting
t = 0; %Variable to store harvest time
%T = randi([2 4]); % time to pick fruit
%W = randi ([5 10]) %Weight of fruit
for i = 1:B
H = A(i);
while H > 0
H = H - randi([5 10]);
t = t + randi([2 4]);
end
end
disp(t)
170

추가 답변 (2개)

Voss
Voss 2023년 3월 5일
Maybe something like this:
% A = T(:,1); %Column vector
A = [71.213; 74.499; 79.175; 54.163; 83.008; 52.615];
B = length(A); %size of column (number of elements)
t = 0; %Variable to store harvest time
for i = 1:B
H = A(i);
while H > 0
H = H - randi([5 10]);
t = t + randi([2 4]);
end
end
disp(t);
167
Note that randi generates random integers. If you want to allow the possibility that the random numbers are not integers, you can use rand instead.

Askic V
Askic V 2023년 3월 5일
편집: Askic V 2023년 3월 5일
A = [71.213; 74.499; 79.175; 54.163; 83.008; 52.615];
B = length(A); %size of column (number of elements)
%H = A(i); %Variable to store remaining harvest, after subtracting
t = 0; %Variable to store harvest time
%T = randi([2 4]); % time to pick fruit
%W = randi ([5 10]) %Weight of fruit
for i = 1:B
while A(i) >= 0
A(i) = A(i)- randi ([5 10]);
t = t + randi([2 4]);
disp(t);
end
end
4 7 10 13 15 19 22 24 26 30 33 37 39 41 45 48 51 53 56 60 64 68 70 74 76 80 83 85 87 90 92 94 96 98 102 104 107 109 111 113 116 119 121 125 127 129 133 137 141 144 148 152 156 159 161 164 167 171 175 177 179
Would this code be what you want?
A
A = 6×1
-4.7870 -7.5010 -5.8250 -0.8370 -0.9920 -3.3850

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by