Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Simple matrix manipulation question
Date: Thu, 16 Jul 2009 21:13:03 +0000 (UTC)
Organization: Battelle Energy Alliance (INL)
Lines: 38
Message-ID: <h3o54v$hc9$1@fred.mathworks.com>
References: <h3lt89$b6e$1@fred.mathworks.com> <h3o275$2h9$1@fred.mathworks.com> <h3o2o3$7g4$1@fred.mathworks.com> <h3o46t$f27$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1247778783 17801 172.30.248.37 (16 Jul 2009 21:13:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 16 Jul 2009 21:13:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: news.mathworks.com comp.soft-sys.matlab:556099


I can see no difference in speed (nan vs. inf), but Matt's solution is incomplete as I understand the problem.


% Data
N = 1200;
A = round(rand(N)*20);
B = randperm(N);

% obvious engine.
tic
for ii = N:-1:1
   C(ii,1) = min(A(ii,1:B(ii)));  % OP wants column vector.
end
toc

% faster engine for this data.
tic
for ii = N:-1:1
    m = A(ii);
    for jj = 1:B(ii)
        if A(ii,jj)<m
            m = A(ii,jj);
        end
    end
   C2(ii,1) = m; 
end
toc

% Unfinished(?) engine.
tic
Amod = A;
Amod( bsxfun(@lt,B,(1:N)) ) = nan;
c = min(Amod,[],2); 
toc

whos
isequal(C,C2)  % Yes
isequal(C,c)  % No