Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: N-dimensional find
Date: Wed, 18 Feb 2009 20:37:01 +0000 (UTC)
Organization: Battelle Energy Alliance (INL)
Lines: 48
Message-ID: <gnhrhd$llc$1@fred.mathworks.com>
References: <gnhibi$s1m$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1234989421 22188 172.30.248.35 (18 Feb 2009 20:37:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 18 Feb 2009 20:37:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: news.mathworks.com comp.soft-sys.matlab:519247


If it is speed you are after (and it sounds like maybe it isn't), it might be worth it to make a sub function with not one, but two loops (blasphemy!).  Try this out, the speed gain will depend on the size of BW and it's sparsity.  Run it a couple of times.



%--------------------------------------------------------------------------%
function [] = find_idx_dim()
% Demonstrate three different techniques for finding the index of the first
% non-zero element of each column in a binary matrix.
BW = round(rand(1000));  % A binary matrix.
% BW = rand(1000)>.9;  % A sparse binary matrix.  Try this out too.
t = zeros(1,3);  % For timings.

tic
[FI1,FI1] =  max(BW); % Technique 1.
t(1) = toc;

tic
FI2 = zeros(1,size(BW,2));  % Technique 2.
[R,C] = find(cumsum(cumsum(BW))==1);
FI2(C) = R;
t(2) = toc;

tic
FI3 = getidx(BW);  % Technique 3, Call subfunction.
t(3) = toc;

% Show the relative times and if all are equal.
fprintf('\n%s%3.1f\t%3.1f\t%3.1f','Relative times:   ',t/min(t)) 
if all(FI1==FI2 & FI1==FI3')  % check for equality.
    fprintf('%s\n\n','    and all are equal')
else
    fprintf('%s\n\n','    and all are not equal')
end

function idx = getidx(BW)
% Subfunction that finds the first 1 index in each column.
[r,c] = size(BW);
idx = zeros(r,1);
for ii = 1:c
    for kk = 1:r
        if BW(kk,ii)==1
            idx(ii) = kk;
            break
        end
    end
end 

%--------------------------------------------------------------------------%