How do I properly use the "while", "for", "loop" command in this problem?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
VBBV
el 6 de Mzo. de 2023
Editada: VBBV
el 7 de Mzo. de 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
Respuesta aceptada
Torsten
el 5 de Mzo. de 2023
Editada: Torsten
el 5 de Mzo. de 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)
Más respuestas (2)
Voss
el 5 de Mzo. de 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 comentarios
Askic V
el 5 de Mzo. de 2023
Editada: Askic V
el 5 de Mzo. de 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
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!