How do I replace this for-loop?
Martin Weiser
weiser2 at natur.cuni.cz
Sat Jul 5 14:08:38 CDT 2008
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 at octave.org
>> https://www.cae.wisc.edu/mailman/listinfo/help-octave
>>
>>
> _______________________________________________
> Help-octave mailing list
> Help-octave at octave.org
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
>
>
More information about the Help-octave
mailing list