How to put the solutions of a equation in a matrix (there are different solutions because variables are changeable), then find the wanted solutions with it's variables?

1 Ansicht (letzte 30 Tage)
If this is my equation:
x = 10*a(i) + b(j) ;
a = [1 2 3] ;
b = [ 3 1 6] ;
& I only want to keep the solution if x is greater than 25.
I want to find all the possible solutions of x and find the minimum or maximum x with the variables of that x.
counter = 0;
for i = 1:3
for j = 1:3
counter = counter + 1;
for k = 1: counter
solutions = zeros (1, counter);
x = 10*a(i) + b(j) ;
if x > 25
solutions (k) = x;
else
continue
end
end
end
end
min (solutions);
But how can I find the variables that creates the minimum solution?
There are more variables and complicated conditions in real so I can not calculate by hand.
I hope I was clear, English is not my mother tongue.
Thanks for your time and I wish you health&safety!

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 24 Sep. 2020
Bearbeitet: Ameer Hamza am 24 Sep. 2020
Try this
a = [1 2 3];
b = [3 1 6];
[A, B] = ndgrid(a, b);
A = A(:);
B = B(:);
x = 10*A + B;
[x_min, idx] = min(x(x>25));
A_sol = A(idx);
B_sol = B(idx);

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by