How do I properly use the "while", "for", "loop" command in this problem?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
VBBV
am 6 Mär. 2023
Bearbeitet: VBBV
am 7 Mär. 2023
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
Akzeptierte Antwort
Torsten
am 5 Mär. 2023
Bearbeitet: Torsten
am 5 Mär. 2023
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)
Weitere Antworten (2)
Voss
am 5 Mär. 2023
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.
2 Kommentare
Askic V
am 5 Mär. 2023
Bearbeitet: Askic V
am 5 Mär. 2023
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
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!