Optimization Simple search
古いコメントを表示
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
2011 年 4 月 20 日
min([ 9 3 2 7 1 8 ])
Optimization?!
回答 (8 件)
Matt Tearle
2011 年 4 月 20 日
1 投票
Can you explain what you want beyond min(x)?
John
2011 年 4 月 20 日
0 投票
12 件のコメント
Matt Tearle
2011 年 4 月 20 日
Why do you want to write your own? Is there something to your application that min can't handle? If it's efficiency, you're not going to be able to beat the built-in min function.
Paulo Silva
2011 年 4 月 20 日
looks like homework or something similar, I know you love logical indexing but please don't hate the loops :)
Matt Tearle
2011 年 4 月 20 日
I'm not hatin' on the loops. It's just the difference between a built-in function and a user-defined function. You'll end up basically writing the same function as the built-in version, with none of the optimization benefit.
Paulo Silva
2011 年 4 月 20 日
that's a good reason to make the loop are compare the performance of it versus the MATLAB functions, might be the best way to learn, also some teachers won't be happy if they ask for a loop and get something else.
Matt Tearle
2011 年 4 月 21 日
That's why I'd really like to know *why* John wants to avoid built-ins. If it's "because the teacher said so", well fine, can't argue with that. But if it's something else like "I can write my own stripped down, lean, mean, min-finding machine that will speed up my code", then it's important to know that it can't be done. (Well, not easily :))
Matt Fig
2011 年 4 月 21 日
It wasn't that hard! It only took me about 30 seconds to write code faster than MIN for vector x...
>> x = rand(1,100000);
>> tic,[mn,I] = min(x);toc
Elapsed time is 0.008383 seconds.
>> tic,[mn,I] = min(x);toc
Elapsed time is 0.009373 seconds.
>> tic,[mn,I] = min(x);toc
Elapsed time is 0.008827 seconds.
>> tic,[mn,I] = min(x);toc
Elapsed time is 0.008746 seconds.
>>
>> tic,[mn2,I2] = mymin(x);toc
Elapsed time is 0.004359 seconds.
>> tic,[mn2,I2] = mymin(x);toc
Elapsed time is 0.004483 seconds.
>> tic,[mn2,I2] = mymin(x);toc
Elapsed time is 0.004266 seconds.
>> tic,[mn2,I2] = mymin(x);toc
Elapsed time is 0.004549 seconds.
>>
>> isequal(mn,mn2)
ans =
1
>> isequal(I,I2)
ans =
1
Paulo Silva
2011 年 4 月 21 日
That's very fast code but John insists on the bubble sort!!! ahahah :)
Paulo Silva
2011 年 4 月 21 日
I tested my code and Johns with
n=100000
m=randi([1 10],n,2); %x and y array
The timings are 0.002s for my code and 231.954s for Johns code, the test values aren't similar to yours but it does provide one idea on how inefficient is the bubble sort method.
Matt Tearle
2011 年 4 月 22 日
@Matt Fig: show your code! I can't beat the built-in. What version are you running? And I assume you have multiple cores (and can make use of implicit multithreading)? Mine's only dual-core anyway. Oh and, not surprisingly, I'm using R2011a.
Matt Fig
2011 年 4 月 22 日
@Matt Tearle: Actually the code is similar to code posted by Paulo Silva. Was I surprised that it is faster than the built-in? Heck yes! But it is on my machine, as shown above. I later even added some feature checking ([] and nan) and it is still faster, though only by 8% instead of nearly 200% as before. I am using a dual core Centrino at 2.33 Ghz on 2007b.
One curiosity I did notice is that MIN with only one output is twice as fast as with two outputs... interesting. Here is the code, but notice not the code I used yesterday. The NaN and [] handling weren't there.
function [mn,I] = mymin(x)
% Find the min for vector x.
% Author: Matt Fig
if isempty(x)
mn = [];
I = [];
return
end
idx = isnan(x);
if idx
mn = nan;
I = 1;
return
else
x(idx) = inf;
end
mn = x(1);
I = 1;
for ii = 2:length(x)
if x(ii)< mn
mn = x(ii);
I = ii;
end
end
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
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
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
2011 年 4 月 20 日
or more loops for
k = sum(abs(m));
for jj = 1:numel(m)
k = min(k,m(jj));
end
Walter Roberson
2011 年 4 月 20 日
0 投票
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
2011 年 4 月 21 日
0 投票
6 件のコメント
Walter Roberson
2011 年 4 月 21 日
Bubble sort is one of the _least_ efficient deterministic sorts, at least for sufficiently large arrays. For very small arrays it can be faster than some of the alternatives.
John
2011 年 4 月 21 日
Matt Tearle
2011 年 4 月 21 日
I thought you just wanted the minimum value -- sorting the entire array just to get the minimum seems highly suboptimal. And, again, can you *please* explain why you want to avoid built-in functions? I'm sure there *are* good reasons, but very often people think they have a good reason that turns out to be false (eg efficiency, deployability, etc).
John
2011 年 4 月 21 日
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
2011 年 4 月 21 日
John
2011 年 4 月 21 日
1 件のコメント
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
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
2011 年 4 月 22 日
4 件のコメント
Walter Roberson
2011 年 4 月 22 日
If you just need to find the minimum, why bother sorting???
John
2011 年 4 月 22 日
Walter Roberson
2011 年 4 月 22 日
No, just a single pass through finding the minimum is *much* easier than sorting.
John
2011 年 4 月 22 日
カテゴリ
ヘルプ センター および File Exchange で Surrogate Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!