The min(y) function where y is a vector

1 visualizzazione (ultimi 30 giorni)
Euan il 12 Nov 2011
If i have used a loop say
for x=1:1:10
y(x)=(x^2)/2
end
then use min(y) to display the minimum from this vector, 0.5. How would I get it to display the corresponding x value?
thanks
0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

Risposte (2)

Fangjun Jiang il 12 Nov 2011
In your code, x is a scalar. It is over-written every iteration in the for-loop.
min() can return the index.
x=1:10;
y=x.^2/2;
[ymin,pos]=min(y);
x_val=x(pos)
0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

Wayne King il 12 Nov 2011
[minval,index] = min(y);
In this case it's trivial since index is the x value at which the minimum occurs. This function is increasing on your x values, so the minimum is at the first element.
0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Operators and Elementary Operations 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!

Translated by