|
View:
New views
11 Messages
—
Rating Filter:
Alert me
|
|
|
How do I replace this for-loop?What's the best way to replace this for-loop?
Given an array, n, of datapoints, such that: >> size(n) rows = 27000 columns = 2 where n(:,1) are all the integer x values, and n(:,2) are all the integer y values, both integer values are in the range of 1 to 101 I slowly did it this way: output=zeros(101,101); for i=1:27000 output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + 1; endfor as you all know this is fairly slow. What's a simple way to replace this for-loop? Robert _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?On Fri, 4 Jul 2008, Robert Macy wrote:
> What's the best way to replace this for-loop? > > Given an array, n, of datapoints, such that: > >>> size(n) > rows = 27000 > columns = 2 > > where n(:,1) are all the integer x values, and > n(:,2) are all the integer y values, > both integer values are in the range of 1 to 101 > > I slowly did it this way: > > output=zeros(101,101); > for i=1:27000 > output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + 1; > endfor > > as you all know this is fairly slow. > What's a simple way to replace this for-loop? > > Robert > what about something like: ni=zeros(27000,1); ni=sub2ind(size(n),n(:,1),n(:,2)); ni=sort(ni); [breaks,garbage,indeces]=find(ni-shift(ni,1); times=breaks-shift(breaks,1); output=zeros(101,101); output(indeces)=times; (I would guess that people here will come with something much more straightforward and elegant, maybe with an one-liner.) I have not tested that, but HTH Martin W. > _______________________________________________ > Help-octave mailing list > Help-octave@... > https://www.cae.wisc.edu/mailman/listinfo/help-octave > > _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
RE: How do I replace this for-loop?Does this give you any speed up? output(n(:,1),n(:,2)) = 1; -- Valmor > -----Original Message----- > From: Robert Macy [mailto:macy@...] > Sent: Friday, July 04, 2008 5:49 PM > To: help-octave@... > Subject: How do I replace this for-loop? > > What's the best way to replace this for-loop? > > Given an array, n, of datapoints, such that: > > >> size(n) > rows = 27000 > columns = 2 > > where n(:,1) are all the integer x values, and > n(:,2) are all the integer y values, > both integer values are in the range of 1 to 101 > > I slowly did it this way: > > output=zeros(101,101); > for i=1:27000 > output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + 1; > endfor > > as you all know this is fairly slow. > What's a simple way to replace this for-loop? > > Robert > > _______________________________________________ > Help-octave mailing list > Help-octave@... > https://www.cae.wisc.edu/mailman/listinfo/help-octave _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?2008/7/5 de Almeida, Valmor F. <dealmeidav@...>:
> output(n(:,1),n(:,2)) = 1; That doesn't produce the desired output, unfortunately. - Jordi G. H. _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?On Sat, 5 Jul 2008, Martin Weiser wrote:
> On Fri, 4 Jul 2008, Robert Macy wrote: > >> What's the best way to replace this for-loop? >> >> Given an array, n, of datapoints, such that: >> >>>> size(n) >> rows = 27000 >> columns = 2 >> >> where n(:,1) are all the integer x values, and >> n(:,2) are all the integer y values, >> both integer values are in the range of 1 to 101 >> >> I slowly did it this way: >> >> output=zeros(101,101); >> for i=1:27000 >> output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + 1; >> endfor >> >> as you all know this is fairly slow. >> What's a simple way to replace this for-loop? >> >> Robert >> > Hi, > what about something like: > ni=zeros(27000,1); > ni=sub2ind(size(n),n(:,1),n(:,2)); > ni=sort(ni); > [breaks,garbage,indeces]=find(ni-shift(ni,1); > times=breaks-shift(breaks,1); > output=zeros(101,101); > output(indeces)=times; > > (I would guess that people here will come with something much more > straightforward and elegant, maybe with an one-liner.) > I have not tested that, but HTH > Martin W. Hello, I am sorry, I have realized now that I was almost completely wrong. Sorry again, Martin W. _______________________________________________ >> Help-octave mailing list >> Help-octave@... >> https://www.cae.wisc.edu/mailman/listinfo/help-octave >> >> > _______________________________________________ > Help-octave mailing list > Help-octave@... > https://www.cae.wisc.edu/mailman/listinfo/help-octave > > Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?On Jul 4, 2008, at 5:48 PM, Robert Macy wrote:
> What's the best way to replace this for-loop? > > Given an array, n, of datapoints, such that: > >>> size(n) > rows = 27000 > columns = 2 > > where n(:,1) are all the integer x values, and > n(:,2) are all the integer y values, > both integer values are in the range of 1 to 101 > > I slowly did it this way: > > output=zeros(101,101); > for i=1:27000 > output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + 1; > endfor Well, there's hist2d :) http://octave.svn.sourceforge.net/viewvc/octave/trunk/octave-forge/ main/plot/inst/hist2d.m?revision=HEAD&content-type=text/plain > > as you all know this is fairly slow. > What's a simple way to replace this for-loop? > > Robert > > _______________________________________________ > Help-octave mailing list > Help-octave@... > https://www.cae.wisc.edu/mailman/listinfo/help-octave -- Rob Mahurin Dept. of Physics & Astronomy University of Tennessee phone: 865 207 2594 Knoxville, TN 37996 email: rob@... _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?On Saturday, July 05, 2008, at 03:20PM, "Martin Weiser" <weiser2@...> wrote: >On Sat, 5 Jul 2008, Martin Weiser wrote: > >> On Fri, 4 Jul 2008, Robert Macy wrote: >> >>> What's the best way to replace this for-loop? >>> >>> Given an array, n, of datapoints, such that: >>> >>>>> size(n) >>> rows = 27000 >>> columns = 2 >>> >>> where n(:,1) are all the integer x values, and >>> n(:,2) are all the integer y values, >>> both integer values are in the range of 1 to 101 >>> >>> I slowly did it this way: >>> >>> output=zeros(101,101); >>> for i=1:27000 >>> output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + 1; >>> endfor >>> >>> as you all know this is fairly slow. >>> What's a simple way to replace this for-loop? >>> >>> Robert >>> >> Hi, >> what about something like: >> ni=zeros(27000,1); >> ni=sub2ind(size(n),n(:,1),n(:,2)); >> ni=sort(ni); >> [breaks,garbage,indeces]=find(ni-shift(ni,1); >> times=breaks-shift(breaks,1); >> output=zeros(101,101); >> output(indeces)=times; >> >> (I would guess that people here will come with something much more >> straightforward and elegant, maybe with an one-liner.) >> I have not tested that, but HTH >> Martin W. > >Hello, >I am sorry, I have realized now that I was almost completely wrong. >Sorry again, >Martin W. Martin, Actually, you weren't so far off. Your attempt gave me the inspiration needed to produce the result below. For testing purposes, I created a random version for "n". N = 27000; M = 101; output = zeros (M, M); n = round ((M-1) * rand([N, 2])) + 1; ni = sub2ind (size (output), n(:,1), n(:,2)); ni = sort (ni); ni = [ni; max(ni)+1]; fdni = find (diff (ni)); num = diff ([0;fdni]); mi = ni(fdni); output(mi) = num; Robert, As I am a 3.0.1/3.0.0+ user, I have no idea if this will work for Octave 2.1.50a. Ben _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?2008/7/5 Ben Abbott <bpabbott@...>:
> Actually, you weren't so far off. Your attempt gave me the inspiration > needed to produce the result below. A winner is you. :-) Your call to find is a little faster than my call to unique. I'm thinking, wouldn't it be faster to simply compile in the original for-loop instead of these acrobatics? - Jordi G. H. _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?It worked,...quite fast, too
Thank you, you've introduced me to new functions here. Robert On Sat, 05 Jul 2008 19:33:48 -0700 Ben Abbott <bpabbott@...> wrote: > > On Saturday, July 05, 2008, at 03:20PM, "Martin Weiser" > <weiser2@...> wrote: > >On Sat, 5 Jul 2008, Martin Weiser wrote: > > > >> On Fri, 4 Jul 2008, Robert Macy wrote: > >> > >>> What's the best way to replace this for-loop? > >>> > >>> Given an array, n, of datapoints, such that: > >>> > >>>>> size(n) > >>> rows = 27000 > >>> columns = 2 > >>> > >>> where n(:,1) are all the integer x values, and > >>> n(:,2) are all the integer y values, > >>> both integer values are in the range of 1 to 101 > >>> > >>> I slowly did it this way: > >>> > >>> output=zeros(101,101); > >>> for i=1:27000 > >>> output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + > 1; > >>> endfor > >>> > >>> as you all know this is fairly slow. > >>> What's a simple way to replace this for-loop? > >>> > >>> Robert > >>> > >> Hi, > >> what about something like: > >> ni=zeros(27000,1); > >> ni=sub2ind(size(n),n(:,1),n(:,2)); > >> ni=sort(ni); > >> [breaks,garbage,indeces]=find(ni-shift(ni,1); > >> times=breaks-shift(breaks,1); > >> output=zeros(101,101); > >> output(indeces)=times; > >> > >> (I would guess that people here will come with > something much more > >> straightforward and elegant, maybe with an one-liner.) > >> I have not tested that, but HTH > >> Martin W. > > > >Hello, > >I am sorry, I have realized now that I was almost > completely wrong. > >Sorry again, > >Martin W. > > Martin, > > Actually, you weren't so far off. Your attempt gave me > the inspiration needed to produce the result below. For > testing purposes, I created a random version for "n". > > N = 27000; > M = 101; > output = zeros (M, M); > n = round ((M-1) * rand([N, 2])) + 1; > ni = sub2ind (size (output), n(:,1), n(:,2)); > ni = sort (ni); > ni = [ni; max(ni)+1]; > fdni = find (diff (ni)); > num = diff ([0;fdni]); > mi = ni(fdni); > output(mi) = num; > > Robert, As I am a 3.0.1/3.0.0+ user, I have no idea if > this will work for Octave 2.1.50a. > > Ben > _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?THANK YOU!
I downloaded a copy of hist2d from the URL you gave me and it works great. Robert On Sat, 5 Jul 2008 15:54:51 -0400 Rob Mahurin <rob@...> wrote: > On Jul 4, 2008, at 5:48 PM, Robert Macy wrote: > > What's the best way to replace this for-loop? > > > > Given an array, n, of datapoints, such that: > > > >>> size(n) > > rows = 27000 > > columns = 2 > > > > where n(:,1) are all the integer x values, and > > n(:,2) are all the integer y values, > > both integer values are in the range of 1 to 101 > > > > I slowly did it this way: > > > > output=zeros(101,101); > > for i=1:27000 > > output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + > 1; > > endfor > > Well, there's hist2d :) > > > main/plot/inst/hist2d.m?revision=HEAD&content-type=text/plain > > > > > as you all know this is fairly slow. > > What's a simple way to replace this for-loop? > > > > Robert > > > > _______________________________________________ > > Help-octave mailing list > > Help-octave@... > > https://www.cae.wisc.edu/mailman/listinfo/help-octave > > > > -- > Rob Mahurin > Dept. of Physics & Astronomy > University of Tennessee phone: 865 207 2594 > Knoxville, TN 37996 email: rob@... > > _______________________________________________ > Help-octave mailing list > Help-octave@... > https://www.cae.wisc.edu/mailman/listinfo/help-octave _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: How do I replace this for-loop?>Given an array, n, of datapoints, such that:
> >>> size(n) >rows = 27000 >columns = 2 > >where n(:,1) are all the integer x values, and n(:,2) are all the >integer y values, both integer values are in the range of 1 to 101 > >I slowly did it this way: > >output=zeros(101,101); >for i=1:27000 > output( n(i,1),n(i,2) ) = output( n(i,1),n(i,2) ) + 1; >endfor You are creating a sparse matrix with summation by hand. It should work like this: output = sparse(n(:,1),n(:2),ones(27000,1),101,101); -- Francesco Potortì (ricercatore) Voice: +39 050 315 3058 (op.2111) ISTI - Area della ricerca CNR Fax: +39 050 315 2040 via G. Moruzzi 1, I-56124 Pisa Email: Potorti@... (entrance 20, 1st floor, room C71) Web: http://fly.isti.cnr.it/ _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
| Free Forum Powered by Nabble | Forum Help |