From: "Christian " <>
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$>
Reply-To: "Christian " <>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1394145308 942 (6 Mar 2014 22:35:08 GMT)
NNTP-Posting-Date: Thu, 6 Mar 2014 22:35:08 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 3858030
Xref: 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 

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,