How to vectorize the find function?

This is my code:
for i=1:size(Z,3)
indmax(i)=find(abs(Z(1,:,i))>z90(1,1,i),1);
indmin(i)=find(abs(Z(1,:,i))>z10(1,1,i),1);
end
trise(:,1)=(t(indmax)-t(indmin));
The variable Z is normally 3 or 6 rows, many columns and 5-10 pages. Something like:
size(Z)= 6 1920 8
The variables z10 and z90 have the same number of rows and pages as Z and 1 column. Something like:
size(z10)=size(z90)=6 1 8
I would actually need to repeat this code 6 more times (1 for each row).
Is there a way to vectorize this?

 Risposta accettata

%small sample because you only looped over first row
Z = rand(1,5,8)*2.5;
z90 = rand(1,1,8);
z10 = rand(1,1,8);
%Your way
for i=1:size(Z,3)
indmax(i)=find(abs(Z(1,:,i))>z90(1,1,i),1);
indmin(i)=find(abs(Z(1,:,i))>z10(1,1,i),1);
end
%Vectozed way:
[jk, indmx] = max(bsxfun(@gt,abs(Z),z90),[],2);
[jk, indmn] = max(bsxfun(@gt,abs(Z),z10),[],2);
indmx = squeeze(indmx);
indmn = squeeze(indmin);
isequal(indmin,indmn)
isequal(indmax,indmx.') %transpose rowvec
This method is extended to the full size, though just looping over the six rows is probably just as easy/fast. I.e. loop 1:6, extract trise on each iteration.

6 Commenti

Matt Fig
Matt Fig il 13 Mag 2011
And the (you knew this was coming, right?) obligatory timings...
function [] = timeitagain
Z = rand(6,1920,8)*2.5;
z90 = rand(6,1,8);
z10 = rand(6,1,8);
% Proper for loop...
tic
for jj = size(Z,1):-1:1
for ii=size(Z,3):-1:1
R = abs(Z(jj,:,ii));
indmax(jj,ii)=find(R>z90(jj,1,ii),1);
indmin(jj,ii)=find(R>z10(jj,1,ii),1);
end
end
toc
%Vectozed way:
tic
[jk, indmx] = max(bsxfun(@gt,abs(Z),z90),[],2);
[jk, indmn] = max(bsxfun(@gt,abs(Z),z10),[],2);
indmx = squeeze(indmx);
indmn = squeeze(indmin);
toc
isequal(indmin,indmn)
isequal(indmax,indmx) %transpose rowvec
Now running from the command line:
Elapsed time is 0.001451 seconds.
Elapsed time is 0.012311 seconds.
At first your results made me sad. But vectorizing consistently wins by more 30% on my system! (I squeezed the ABS out of both of your and my bsx).
function [t1 t2] = timeitagain
Z = rand(6,1920,8)*2.5;
z90 = rand(6,1,8);
z10 = rand(6,1,8);
% Proper for loop...
tic
Z2 = abs(Z);
for jj = size(Z,1):-1:1
for ii=size(Z,3):-1:1
R = Z2(jj,:,ii);
indmax(jj,ii)=find(R>z90(jj,1,ii),1);
indmin(jj,ii)=find(R>z10(jj,1,ii),1);
end
end
t1 = toc;
%Vectozed way:
tic
Z3 = abs(Z);
[~, indmx] = max(bsxfun(@gt,Z3,z90),[],2);
[~, indmn] = max(bsxfun(@gt,Z3,z10),[],2);
indmx = squeeze(indmx);
indmn = squeeze(indmn);
t2 = toc;
if ~isequal(indmin,indmn) || ~isequal(indmax,indmx)
error('Sean you failed vectorizing!');
end
Called with:
t1 = 0;
t2 = 0;
for ii = 1:10
[t1c t2c] = timeitagain;
t1 = t1+t1c;
t2 = t2+t2c;
end
t1/t2
%{
ans =
1.3154
ans =
1.3181
ans =
1.3993
ans =
1.3189
%}
Ps. How's your new laptop?
Matt Fig
Matt Fig il 13 Mag 2011
Figures, it must be improvements in newer versions. I am still using 2007b.
My new laptop was delayed a day. "Out for delivery" today though!!
(P.S. Lenovo w520 fully loaded...)
^^^^Jealous.
Thanks for the help!

Accedi per commentare.

Più risposte (1)

Doug Hull
Doug Hull il 13 Mag 2011

0 voti

Why vectorize?
Just add another for loop for the rows.

4 Commenti

The problem is that this section is inside a bigger loop that repeats 13770 times. This part added to the loop for the rows is adding about 10min to the whole simulation, which takes about 40min (without this section). I was wondering if I could make it faster.
Matt Fig
Matt Fig il 13 Mag 2011
You will find that pre-allocating your arrays will speed your code up tremendously if your data is that large.
Doug Hull
Doug Hull il 13 Mag 2011
Your question is then about speeding up the code, not about vectorizing?
Yeah, sorry about that.. It's just that vectorizing usually speeds up the code.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by