# Choose the lowest value and jump into the correct if

1 visualizzazione (ultimi 30 giorni)
Martin il 11 Giu 2021
Risposto: Chunru il 11 Giu 2021
I got variables like:
A = 1;
B = 2;
C = 3;
those values are random and can change over time. I want to jump into the correct area in the if-statements (or case switch) for the lowest value of A,B,C. Something like
if lowest(A,{A,B,C})
do_something_A = true;
elseif lowest(B,{A,B,C})
do_something_B = true;
elseif lowest(C,{A,B,C})
do_something_C = true;
end
This is a "self-invented" function, but hope you get my point
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Chunru il 11 Giu 2021
A=1; B=2; C=3;
z = [A, B, C]; % put variables in a vector
[~, idx] = min(z); % idx tells which variable is the minimum
% Then you can use idx to control what you want to do.
% As an example, you can use switch.
switch idx
case 1 % A is minimum
fprintf('A is the smallest')
case 2
fprintf('B is the smallest')
case 3
fprintf('C is the smallest')
end
A is the smallest
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Jonas il 11 Giu 2021
use something like
[~,idx]=min([A B C]);
if idx==1 % A smallest
elseif idx==2 % B smallest
else % C snallest
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by