indexing question #6

  • Follow


Suppose I have an mx2 array of row,col values. What is a good way to
zero out elements at those locations in an array arr?
0
Reply yaroslavvb (129) 9/29/2009 7:36:46 AM

"Yaroslav Bulatov" <yaroslavvb@gmail.com> wrote in message 
news:f0e49a6a-a509-4fe2-b12e-3a5e4f1ea634@a37g2000prf.googlegroups.com...
> Suppose I have an mx2 array of row,col values. What is a good way to
> zero out elements at those locations in an array arr?
>

You mean 'arr' is a linear array of indices? as described in ind2sub? is so 
try this:

EDU>> A=rand(3)
A =

    0.6991    0.5472    0.2575
    0.8909    0.1386    0.8407
    0.9593    0.1493    0.2543

arr=[1,4,8];  %linear indices
A(arr)=0


A =

         0         0    0.2575
    0.8909    0.1386         0
    0.9593    0.1493    0.2543

--Nasser



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4465 (20090928) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




0
Reply Nasser 9/29/2009 8:01:48 AM


Yaroslav Bulatov <yaroslavvb@gmail.com> wrote in message <f0e49a6a-a509-4fe2-b12e-3a5e4f1ea634@a37g2000prf.googlegroups.com>...
> Suppose I have an mx2 array of row,col values. What is a good way to
> zero out elements at those locations in an array arr?

arr(  sub2ind(size(arr), mx2(:,1), mx2(:,2)    )   )=0
0
Reply xys (1072) 9/29/2009 8:04:01 AM

On Sep 29, 1:04=A0am, "Matt " <x...@whatever.com> wrote:
> Yaroslav Bulatov <yarosla...@gmail.com> wrote in message <f0e49a6a-a509-4=
fe2-b12e-3a5e4f1ea...@a37g2000prf.googlegroups.com>...
> > Suppose I have an mx2 array of row,col values. What is a good way to
> > zero out elements at those locations in an array arr?
>
> arr( =A0sub2ind(size(arr), mx2(:,1), mx2(:,2) =A0 =A0) =A0 )=3D0

Thanks, that works. Interestingly, for a 480x480 arr and 100x2 mx2
this method is about 20 times slower (using single tic/toc
measurement) than looping over all rows of mx2, and setting
corresponding entries to 0 as below

  for j=3D1:size(mx2,1)
      arr(mx2(j,1),mx2(j,2))=3D0;
  end
0
Reply yaroslavvb (129) 10/1/2009 12:41:00 AM

Yaroslav Bulatov <yaroslavvb@gmail.com> wrote in message <16920ae7-dfa4-44cc-af23-88d800f55041@f18g2000prf.googlegroups.com>...
> On Sep 29, 1:04?am, "Matt " <x...@whatever.com> wrote:
> > Yaroslav Bulatov <yarosla...@gmail.com> wrote in message <f0e49a6a-a509-4fe2-b12e-3a5e4f1ea...@a37g2000prf.googlegroups.com>...
> > > Suppose I have an mx2 array of row,col values. What is a good way to
> > > zero out elements at those locations in an array arr?
> >
> > arr( ?sub2ind(size(arr), mx2(:,1), mx2(:,2) ? ?) ? )=0
> 
> Thanks, that works. Interestingly, for a 480x480 arr and 100x2 mx2
> this method is about 20 times slower (using single tic/toc
> measurement) than looping over all rows of mx2, and setting
> corresponding entries to 0 as below
> 
>   for j=1:size(mx2,1)
>       arr(mx2(j,1),mx2(j,2))=0;
>   end


True, but there's a few things to keep in mind

(1) Did you generate mx2 using the find(LogicalMap) function? If you did, it was unnecessary, and is the only reason my proposed approach is required. You could have skipped creating mx2 at all and  just done arr(LogicalMap)=0, which has much less overhead.

(2) size(mx2,1)=100 is a very small number of coordinates. As it gets larger, the gap in speeds gets narrower.

(3) sub2ind.m is implemented in slow M-code, so it's not all that surprising that a for loop is better...
0
Reply xys (1072) 10/1/2009 1:36:02 AM

4 Replies
75 Views

(page loaded in 0.173 seconds)

Similiar Articles:













7/28/2012 5:29:00 AM


Reply: