for loop, if loop, while loop or combination?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Chris Ron de
il 11 Set 2020
Commentato: Chris Ron de
il 14 Set 2020
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 Commenti
Mario Malic
il 12 Set 2020
% 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.
Risposta accettata
Ayush Gupta
il 14 Set 2020
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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Type Identification in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!