필터 지우기
필터 지우기

Optimization Simple search

조회 수: 7 (최근 30일)
John
John 2011년 4월 20일
I got a quick question, lets say I have an array of numbers for example [ 9 3 2 7 1 8 ] and I want to find the minimum value ie: for this example it would be 1.
How could I implement an optimization search like this??
  댓글 수: 1
Paulo Silva
Paulo Silva 2011년 4월 20일
min([ 9 3 2 7 1 8 ])
Optimization?!

댓글을 달려면 로그인하십시오.

답변 (8개)

Matt Tearle
Matt Tearle 2011년 4월 20일
Can you explain what you want beyond min(x)?

John
John 2011년 4월 20일
I know I can use [value index] = min(array) however I am wanting to write my own loop for this.
I just want the loop to output the minimum value in the array, that is it.
  댓글 수: 12
Matt Fig
Matt Fig 2011년 4월 22일
I just noticed that my function incorrectly handles NaN's if the input has only NaN and Inf - the index is always 1. This must be part of the reason why MIN is so much slower with two outputs... It is doing some gymnastics when NaN's are encountered.
Matt Tearle
Matt Tearle 2011년 4월 22일
Interesting! Thanks for the info. I did idly wonder if the number of outputs was making the difference, but couldn't see any reason why. Your explanation makes sense.

댓글을 달려면 로그인하십시오.


Paulo Silva
Paulo Silva 2011년 4월 20일
m=[9 3 2 7 1 8 ]
%another way to find minimum value, using unique
u=unique(m);
u(1)
%another way to find minimum value, using sort
s=sort(m);
s(1)
%another way to find minimum value, a loop this time
MinVal=m(1);
for lo=1:numel(m)
if m(lo)<MinVal
MinVal=m(lo);
end
end
MinVal
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2011년 4월 20일
or more loops for
k = sum(abs(m));
for jj = 1:numel(m)
k = min(k,m(jj));
end

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 4월 20일
If you use one of the optimizers that do not require continuous derivatives, then for any x value, take K = floor(x); if that is out of the range 1:length(m) return +infinity, and otherwise return m(K)
The optimization routine should then find the minimum value, and floor() of the x value it returns will be the index of that value.

John
John 2011년 4월 21일
I have been looking at everything suggested and researched other options is a bubble sort an efficient way to sort an array as well? I am trying to stay away from built in matlab functions
  댓글 수: 6
Paulo Silva
Paulo Silva 2011년 4월 21일
m=randi([1 10],5,2) %x and y array
MinVal=m(1,2);
MinValRow=1;
for lo=1:size(m,1)
if m(lo,2)<MinVal
MinVal=m(lo,2);
MinValRow=lo;
end
end
MinValy=MinVal
MinValx=m(MinValRow,1)
MinValRow
John
John 2011년 4월 21일
Thanks a lot!

댓글을 달려면 로그인하십시오.


John
John 2011년 4월 21일
I am trying to implement this form of a bubble sort, doing what I explained above but matlab is giving me an error because y is not defined in my function, but I do not understand why.
function y= bubble(x,y)
n = length(x);
for k = 1:n-1
for j = 1:n-k
if(x(j)> x(j+1))
temp = x(j);
x(j) = x(j+1);
x(j+1) = temp;
temp = y(j);
y(j) = y(j+1);
y(j+1) = temp;
end % if
end % for
end % for
y = x;
  댓글 수: 1
Paulo Silva
Paulo Silva 2011년 4월 21일
even after being warned about the inefficiency of that method you insist on using it!
Your function does work fine:
m=randi([1 10],10,2) %x and y array
y=bubble(m(:,1),m(:,2))
y(1) %min value

댓글을 달려면 로그인하십시오.


Paulo Silva
Paulo Silva 2011년 4월 21일
John code:
function y= bubble(x,y)
n = length(x);
for k = 1:n-1
for j = 1:n-k
if(x(j)> x(j+1))
temp = x(j);
x(j) = x(j+1);
x(j+1) = temp;
temp = y(j);
y(j) = y(j+1);
y(j+1) = temp;
end % if
end % for
end % for
y = x;
my code:
MinVal=m(1,2);
MinValRow=1;
for lo=1:size(m,1)
if m(lo,2)<MinVal
MinVal=m(lo,2);
MinValRow=lo;
end
end
Test with
n=1:2000
m=randi([1 10],n,2); %x and y array
You can see the time of code execution rising very fast with the bubble sort method, while with my code the time remains almost constant, better codes do exist and I bet that Matt Fig has better code :)

John
John 2011년 4월 22일
I understand your method is more efficient, and i do not believe my bubble sort is working either because I have:
sort=bubble(y1,x1)
To get real specific of what i want I am going to add some code below. Maybe you will see what is going on.
f=@(Isp) (alpha*g*T*Isp)./(2*(1-(((Isp-5000).^2)/(5000^2)))) + Mo*(1-exp(-dV./(Isp*g)));
Analytical_Derivative=@(Isp) 2943./100000./(2-1./12500000*(Isp-5000).^2)-2943./100000.*Isp./(2-1/12500000.*(Isp-5000).^2).^2.*(-1./6250000.*Isp+1./1250)-70000000000/981./Isp.^2.*exp(-1400000/981./Isp);
dydx=@(Isp,y) 2943./100000./(2-1./12500000*(Isp-5000).^2)-2943./100000.*Isp./(2-1/12500000.*(Isp-5000).^2).^2.*(-1./6250000.*Isp+1./1250)-70000000000/981./Isp.^2.*exp(-1400000/981./Isp);
[x1 y1]=eulode(dydx, [1 9700],50000,10);
The eulode is just a program I have written for Euler's method to approximate an integral.
The thing is it is outputting a directory of x-values, as well as y-values. The reason I want to sort the values it that at the point where my y-value is minimum I want to know my x-cordinate, I am trying to do it without a matlab built in function. I am just having the problem of my x values not sorting with each corresponding y value. I know the bubble sore is inefficient but it gets the job done for what I need. Please any suggestion on how to edit my bubble sort code to allow each x value to sort with its corresponding y value, NOT BOTH.
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 4월 22일
No, just a single pass through finding the minimum is *much* easier than sorting.
John
John 2011년 4월 22일
Ok, I'm gonna try to come up with something that will just find a min value then.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by