# for loop, if loop, while loop or combination?

7 ビュー (過去 30 日間)
Chris Ron de 2020 年 9 月 11 日
コメント済み: Chris Ron de 2020 年 9 月 14 日
Hi,
I am stuck with a problem which I thought was quite easy, but it didn't work out so until now.
I will briefly explain the idea.
I have four numbers as a starting point and whatever change I want to make in the numbers, the total sum of the numbers always have to be the same.
With each of the four points I have to make the same calculation and add the results. When the calculation with the four points is completed, I have to change the four numbers and do the calculation again. When the new added result is higher than the former one, the new result have to be the chosen as solution.
I will try to explain it with an example:
I have the values [2,2,2,2] as a startingpoint. With formula A, I get four different values. These values add up to B.
When I take the values [2.5, 1.5, 2, 2], I can do the calculations again. The new calculated value of B is larger than the old one, so [2.5, 1.5, 2, 2] will be my new starting point.
Next I take the values [2.5, 2, 1.5, 2], but they don't give an improvement of B, so I go on with [2.5, 2, 2, 1.5] which also is not an improvement.
I can go on with [1.5, 2.5, 2, 2], [2, 2.5, 1.5, 2], [2, 2.5, 2, 1.5] etc, but I think at this point a for loop will be necessary. I have some problem finding it while I have to add and subtract and change positions.
Moreover, I don't want my numbers to be negative. So when one of the values is 0, the loop cannot subtract from 0. I don't know if I have to use I while loop for this or an if-else-elseif.
Can someone help me with this problem?

#### 4 件のコメント

Chris Ron de 2020 年 9 月 11 日
Thank you very much for your quick response and I think I did not explain my problem enough. Therefore is give a part of the code in which the problem is written without loops etc. It looks very ugly and can be simplified, but that's one of my problems at the moment. Simplifications are causing a non-working script.
det_rate = [0.3, 0.2, 0.5, 0.1];
T = 8; % T is total searching time
delta = 0.5; % take delta from one value of t_start and add it to another value
t = [2 2 2 2; % starting point
2.5 1.5 2 2; % add delta to first and subtract delta from second value
2.5 2 1.5 2; % add delta to first and subtract delta from third value
2.5 2 2 1.5; % add delta to first and subtract delta from fourth value
1.5 2.5 2 2; % add delta to second and subtract delta from first value
2 2.5 1.5 2; % add delta to second and subtract delta from third value
2 2.5 2 1.5; % add delta to second and subtract delta from fourth value
1.5 2 2.5 2; % add delta to third and subtract delta from first value
2 1.5 2.5 2; % add delta to third and subtract delta from second value
2 2 2.5 1.5; % add delta to third and subtract delta from fourth value
1.5 2 2 2.5; % add delta to fourth and subtract delta from first value
2 1.5 2 2.5; % add delta to fourth and subtract delta from second value
2 2 1.5 2.5]; % add delta to fourth and subtract delta from third value
% Doing calculations
P(1,:) = 1-exp(-1.*det_rate.*t(1,:));
P(2,:) = 1-exp(-1.*det_rate.*t(2,:));
P(3,:) = 1-exp(-1.*det_rate.*t(3,:));
P(4,:) = 1-exp(-1.*det_rate.*t(4,:));
P(5,:) = 1-exp(-1.*det_rate.*t(5,:));
P(6,:) = 1-exp(-1.*det_rate.*t(6,:));
P(7,:) = 1-exp(-1.*det_rate.*t(7,:));
P(8,:) = 1-exp(-1.*det_rate.*t(8,:));
P(9,:) = 1-exp(-1.*det_rate.*t(9,:));
P(10,:) = 1-exp(-1.*det_rate.*t(10,:));
P(11,:) = 1-exp(-1.*det_rate.*t(11,:));
P(12,:) = 1-exp(-1.*det_rate.*t(12,:));
P(13,:) = 1-exp(-1.*det_rate.*t(13,:));
s(1,1) = sum(P(1,:));
s(2,1) = sum(P(2,:));
s(3,1) = sum(P(3,:));
s(4,1) = sum(P(4,:));
s(5,1) = sum(P(5,:));
s(6,1) = sum(P(6,:));
s(7,1) = sum(P(7,:));
s(8,1) = sum(P(8,:));
s(9,1) = sum(P(9,:));
s(10,1) = sum(P(10,:));
s(11,1) = sum(P(11,:));
s(12,1) = sum(P(12,:));
s(13,1) = sum(P(13,:));
% new startingpoint (highest value of s), t_new = [2 2 2.5 1.5]
% This new starting value is manualy taken from the values in the workspace
% and not automatically the new starting value in te script
t_new = [2 2 2.5 1.5; % new starting point
2.5 1.5 2.5 1.5; % add to first, subtract from second
2.5 2 2 1.5; % add to first, subtract from third
2.5 2 2.5 1; % add to first, subtract from fourth
1.5 2.5 2.5 1.5; % add to second, subtract from first
2 2.5 2 1.5; % add to second, subtract from third
2 2.5 2.5 1; % add to second, subtract from fourth
1.5 2 3 1.5; % add to third, subtract from first
2 1.5 3 1.5; % add to third, subtract from second
2 2 3 1; % add to third, subtract from fourth
1.5 2 2.5 2; % add to fourth, subtract from first
2 1.5 2.5 2; % add to fourth, subtract from second
2 2 2 2]; % add to fourth, subtract from third
% last value is not necessary, it already failed earlier
P_new(1,:) = 1-exp(-1.*det_rate.*t_new(1,:));
P_new(2,:) = 1-exp(-1.*det_rate.*t_new(2,:));
P_new(3,:) = 1-exp(-1.*det_rate.*t_new(3,:));
P_new(4,:) = 1-exp(-1.*det_rate.*t_new(4,:));
P_new(5,:) = 1-exp(-1.*det_rate.*t_new(5,:));
P_new(6,:) = 1-exp(-1.*det_rate.*t_new(6,:));
P_new(7,:) = 1-exp(-1.*det_rate.*t_new(7,:));
P_new(8,:) = 1-exp(-1.*det_rate.*t_new(8,:));
P_new(9,:) = 1-exp(-1.*det_rate.*t_new(9,:));
P_new(10,:) = 1-exp(-1.*det_rate.*t_new(10,:));
P_new(11,:) = 1-exp(-1.*det_rate.*t_new(11,:));
P_new(12,:) = 1-exp(-1.*det_rate.*t_new(12,:));
P_new(13,:) = 1-exp(-1.*det_rate.*t_new(13,:));
s_new(1,1) = sum(P_new(1,:));
s_new(2,1) = sum(P_new(2,:));
s_new(3,1) = sum(P_new(3,:));
s_new(4,1) = sum(P_new(4,:));
s_new(5,1) = sum(P_new(5,:));
s_new(6,1) = sum(P_new(6,:));
s_new(7,1) = sum(P_new(7,:));
s_new(8,1) = sum(P_new(8,:));
s_new(9,1) = sum(P_new(9,:));
s_new(10,1) = sum(P_new(10,:));
s_new(11,1) = sum(P_new(11,:));
s_new(12,1) = sum(P_new(12,:));
s_new(13,1) = sum(P_new(13,:));
% new startingpoint (highest value of s_new), t_new_new = [2.5 2 2.5 1]
% This new starting value is manualy taken from the values in the workspace
% and not automatically the new starting value in te script
t_new_new = [2.5 2 2.5 1;
3 1.5 2.5 1;
3 2 2 1;
3 2 2.5 0.5;
2 2.5 2.5 1;
2.5 2.5 2 1;
2.5 2.5 2.5 0.5;
2 2 3 1;
2.5 1.5 3 1;
2.5 2 3 0.5;
2 2 2.5 1.5;
2.5 1.5 2.5 1.5;
2.5 2 2 1.5];
P_new_new(1,:) = 1-exp(-1.*det_rate.*t_new_new(1,:));
P_new_new(2,:) = 1-exp(-1.*det_rate.*t_new_new(2,:));
P_new_new(3,:) = 1-exp(-1.*det_rate.*t_new_new(3,:));
P_new_new(4,:) = 1-exp(-1.*det_rate.*t_new_new(4,:));
P_new_new(5,:) = 1-exp(-1.*det_rate.*t_new_new(5,:));
P_new_new(6,:) = 1-exp(-1.*det_rate.*t_new_new(6,:));
P_new_new(7,:) = 1-exp(-1.*det_rate.*t_new_new(7,:));
P_new_new(8,:) = 1-exp(-1.*det_rate.*t_new_new(8,:));
P_new_new(9,:) = 1-exp(-1.*det_rate.*t_new_new(9,:));
P_new_new(10,:) = 1-exp(-1.*det_rate.*t_new_new(10,:));
P_new_new(11,:) = 1-exp(-1.*det_rate.*t_new_new(11,:));
P_new_new(12,:) = 1-exp(-1.*det_rate.*t_new_new(12,:));
P_new_new(13,:) = 1-exp(-1.*det_rate.*t_new_new(13,:));
s_new_new(1,1) = sum(P_new_new(1,:));
s_new_new(2,1) = sum(P_new_new(2,:));
s_new_new(3,1) = sum(P_new_new(3,:));
s_new_new(4,1) = sum(P_new_new(4,:));
s_new_new(5,1) = sum(P_new_new(5,:));
s_new_new(6,1) = sum(P_new_new(6,:));
s_new_new(7,1) = sum(P_new_new(7,:));
s_new_new(8,1) = sum(P_new_new(8,:));
s_new_new(9,1) = sum(P_new_new(9,:));
s_new_new(10,1) = sum(P_new_new(10,:));
s_new_new(11,1) = sum(P_new_new(11,:));
s_new_new(12,1) = sum(P_new_new(12,:));
s_new_new(13,1) = sum(P_new_new(13,:));
% new startingpoint (highest value of s_new_new), t_etc = [3 2 2.5 0.5]
Mario Malic 2020 年 9 月 12 日
% All of commands can be replaced by a single line
P = 1-exp(-1.*det_rate.*t)
s = sum(P,2) % Sums the rows
%Similarly for the other commands
I'd highly recommend you to do MATLAB Onramp before moving further, as it'll help you a lot.
Chris Ron de 2020 年 9 月 14 日
Thank yor for your answer Mario. Most of the topics in the Onramp were already known, but I did learn some new things.

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

### 採用された回答

Ayush Gupta 2020 年 9 月 14 日
Refer to the following code on how to do this:
det_rate = [0.3, 0.2, 0.5, 0.1];
T = 8; % T is total searching time
delta = 0.5; % take delta from one value of t_start and add it to another value
a= 0;
b=0;
c=0;
d=0;
number_of_iterations = T/delta;
max_B = 0;
P = zeros(length(t),4);
s = zeros(length(t),1);
for i= 1:number_of_iterations
for j = 1:number_of_iterations
for k = 1:number_of_iterations
for l=1:number_of_iterations
d = T - (a+b+c);
if(d>0)
result = 1-exp(-1.*det_rate.*t(i,:));
if(sum(result) > max_B)
max_B = sum(result);
end
end
end
c = c+delta;
end
b = b+delta;
end
a = a+delta;
end
Refer to this documentation page to see how for loop works, here.

#### 2 件のコメント

Chris Ron de 2020 年 9 月 14 日
Thank you for your help and the understanding of the written code in which I tried to explain the heuristic problem I was dealing with. I was struggeling but the solution with two if-statements works.
Chris Ron de 2020 年 9 月 14 日
Hi Ayush,
I think I cheared to early. Your example choses all the different possibilities my script have to run, but I think with another heuristic aproach. What I wanted to do was making a new value of t in which one of the values in the vector is added with delta and subsequently the other values are subtracted with delta.
So the new values of t will become in order appearing:
t(first) = t(1)+delta, t(2)-delta, t(3), t(4)
t(second) = t(1)+delta, t(2), t(3)-delta, t(4)
t(third) = t(1)+delta, t(2), t(3), t(4)-delta
t(fourth) = t(1)-delta, t(2)+delta, t(3), t(4)
t(fifth) = t(1), t(2)+delta, t(3)-delta, t(4)
etc.
I have some difficulty in finding the way to do this with for-loops. Is there something I overlooked or is this not possible with Matlab in the way I want to do it.
Another possibility is that I don't understand the way you solved it in your example. In that case, sorry for that from this rookie.

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

### Community Treasure Hunt

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

Start Hunting!