Path: news.mathworks.com!not-for-mail
From: "Christian " <proechri@umich.edu>
Newsgroups: comp.soft-sys.matlab
Subject: fast way to get second output of min
Date: Thu, 6 Mar 2014 22:35:08 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 16
Message-ID: <lfat6s$te$1@newscl01ah.mathworks.com>
Reply-To: "Christian " <proechri@umich.edu>
NNTP-Posting-Host: rubyext-02-ls.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1394145308 942 172.20.102.178 (6 Mar 2014 22:35:08 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 6 Mar 2014 22:35:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 3858030
Xref: news.mathworks.com comp.soft-sys.matlab:810395

Hi all,

I'm looking for a faster way for the following code:

    for k1=1:K1
        for k21=1:K2
            for k22=1:K2
                [~, xind(k1,k21,k22)] = min(abs(yp(k1,k21,k22)-x)); %nearest neighbor of yp in x 
            end
        end
    end

where x is a vector of dimension 1:K1. I have to do this operation zillions of times, so it might be nice to have a speedup. Converting it to a mex file helps a bit, but still it's too slow for what I need. Maybe one could leverage on the fact that I only need the second output element of min.

Thanks a lot,
Christian