How do I properly use the "while", "for", "loop" command in this problem?
1 次查看(过去 30 天)
显示 更早的评论
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
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
采纳的回答
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)
更多回答(2 个)
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);
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
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
Would this code be what you want?
A
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!