フィルターのクリア

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.
  2 件のコメント
Uche
Uche 2023 年 3 月 5 日
Thanks! Got it
Voss
Voss 2023 年 3 月 8 日
編集済み: Voss 2023 年 3 月 8 日
You're welcome!

サインインしてコメントする。


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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by