Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Yet another Vectorization Question

Subject: Yet another Vectorization Question

From: Susan

Date: 17 Feb, 2010 20:38:26

Message: 1 of 8

Does anyone have any thoughts on speeding up the following code?

Thanks!
Susan
-------------------------------------------------
function ix = findfirstinrow( boolMat )

ix = zeros(size(boolMat,1),1);

for row = 1:size(boolMat,1)
  for col = size(boolMat,2)
    ix(row) = find(boolMat(row,:),1);
  end
end

Subject: Yet another Vectorization Question

From: Sadik

Date: 17 Feb, 2010 20:52:06

Message: 2 of 8

Yes, you should at least remove the inner for loop since it does not serve any purpose.

Best.

"Susan " <foowidget@gmail.com> wrote in message <hlhk42$s84$1@fred.mathworks.com>...
> Does anyone have any thoughts on speeding up the following code?
>
> Thanks!
> Susan
> -------------------------------------------------
> function ix = findfirstinrow( boolMat )
>
> ix = zeros(size(boolMat,1),1);
>
> for row = 1:size(boolMat,1)
> for col = size(boolMat,2)
> ix(row) = find(boolMat(row,:),1);
> end
> end

Subject: Yet another Vectorization Question

From: dpb

Date: 17 Feb, 2010 20:56:15

Message: 3 of 8

Susan wrote:
> Does anyone have any thoughts on speeding up the following code?
>
> Thanks!
> Susan
> -------------------------------------------------
> function ix = findfirstinrow( boolMat )
>
> ix = zeros(size(boolMat,1),1);
>
> for row = 1:size(boolMat,1)
> for col = size(boolMat,2)
> ix(row) = find(boolMat(row,:),1);
> end
> end

Why the loop on col; nothing is dependent on it???

But, what's wrong w/

function ix = findfirstinrow( x )
   ix = zeros(size(x,1),1);
   [r,c] = find(x,1,'first');
   ix(r) = 1;

--

Subject: Yet another Vectorization Question

From: Walter Roberson

Date: 17 Feb, 2010 21:06:26

Message: 4 of 8

Susan wrote:
> Does anyone have any thoughts on speeding up the following code?
>
> Thanks!
> Susan
> -------------------------------------------------
> function ix = findfirstinrow( boolMat )
>
> ix = zeros(size(boolMat,1),1);
>
> for row = 1:size(boolMat,1)
> for col = size(boolMat,2)
> ix(row) = find(boolMat(row,:),1);
> end
> end

The following has NOT been tested for timing

function ix = findfirstinrow( boolMat )
   ix = size(boolMat,2) + 1 - sum(cumsum(BoolMat,2) > 0,2);
end

Subject: Yet another Vectorization Question

From: Susan

Date: 17 Feb, 2010 21:32:21

Message: 5 of 8

"Sadik " <sadik.hava@gmail.com> wrote in message <hlhktm$ka0$1@fred.mathworks.com>...
> Yes, you should at least remove the inner for loop since it does not serve any purpose.
>
> Best.

Oops! Thanks.

Subject: Yet another Vectorization Question

From: Susan

Date: 17 Feb, 2010 21:50:26

Message: 6 of 8

Nice! thanks!

Walter Roberson <roberson@hushmail.com> wrote in message
> The following has NOT been tested for timing
>
> function ix = findfirstinrow( boolMat )
> ix = size(boolMat,2) + 1 - sum(cumsum(BoolMat,2) > 0,2);
> end

Subject: Yet another Vectorization Question

From: dpb

Date: 17 Feb, 2010 21:49:18

Message: 7 of 8

dpb wrote:
> Susan wrote:
>> Does anyone have any thoughts on speeding up the following code?
>>
>> Thanks!
>> Susan
>> -------------------------------------------------
>> function ix = findfirstinrow( boolMat )
>>
>> ix = zeros(size(boolMat,1),1);
>>
>> for row = 1:size(boolMat,1)
>> for col = size(boolMat,2)
>> ix(row) = find(boolMat(row,:),1);
>> end
>> end
>
> Why the loop on col; nothing is dependent on it???
>
> But, what's wrong w/
>
> function ix = findfirstinrow( x )
> ix = zeros(size(x,1),1);
> [r,c] = find(x,1,'first');
> ix(r) = 1;

Oh, I guess it's

ix(r)=c;

that you want, isn't it?

--

Subject: Yet another Vectorization Question

From: Jos (10584)

Date: 18 Feb, 2010 14:24:22

Message: 8 of 8

"Susan " <foowidget@gmail.com> wrote in message <hlhk42$s84$1@fred.mathworks.com>...
> Does anyone have any thoughts on speeding up the following code?
>
> Thanks!
> Susan
> -------------------------------------------------
> function ix = findfirstinrow( boolMat )
>
> ix = zeros(size(boolMat,1),1);
>
> for row = 1:size(boolMat,1)
> for col = size(boolMat,2)
> ix(row) = find(boolMat(row,:),1);
> end
> end

Here is another approach

% create test data
% last two rows are special cases
boolmat = [0 0 1 ; 1 0 1 ; 0 1 1 ; 0 1 0 ; 1 1 1 ; 0 0 0]

% engine, be sure we are working with a logical array
  [m,ix] = min(-logical(boolmat),[],2) ; % note the minus sign!
  ix(m==0) = NaN ; % treat special case of all zeros row

% result
  disp([ix boolmat])

hth
Jos

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us