[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: sort indexing
From: |
Nicholas Jankowski |
Subject: |
Re: sort indexing |
Date: |
Mon, 15 Feb 2016 19:43:28 -0500 |
On Feb 15, 2016 4:10 PM, "Daniel J Sebald" <address@hidden> wrote:
>
> On 02/15/2016 02:39 PM, Doug Stewart wrote:
>>
>>
>> I have a question about indexing.
>>
>> a=randi(9,5)
>> [b i]=sort(a)
>> c=a(i)
>>
>> I would think that c should be the same as b, but it is not.
>>
>> The index array i has all the correct information in it as can be seen with
>>
>> for k=1:columns(a)
>> w(:,k)=a(i(:,k),k);
>> endfor
>> now b-w is equal to 0
>>
>> Is there some technical reason that if you try and use the index array
>> i on an matrix
>> of the same dimensions, that it can't work?
>> I would think that it should apply each col of the i to a in a(i)
>> as I did in the loop.
>>
>> I know it can be vectorized:
>>
>> a(sub2ind (size(a), i, repmat(1:4, rows(a), 1)))
>> a(i+(0:columns(a)-1)*rows(a))
>>
>> but I just think that octave should be smart enough to just do a(i)
>>
>> Doug
>
>
> Doug,
>
> I looks like a() is being vectorized when accessed as a(i):
>
> octave:1> a = [0:4] + 5*[0:4]'
> a =
>
> 0 1 2 3 4
> 5 6 7 8 9
> 10 11 12 13 14
> 15 16 17 18 19
> 20 21 22 23 24
>
> octave:2> i = a + 1
> i =
>
> 1 2 3 4 5
> 6 7 8 9 10
> 11 12 13 14 15
> 16 17 18 19 20
> 21 22 23 24 25
>
> octave:3> a(i)
> ans =
>
> 0 5 10 15 20
> 1 6 11 16 21
> 2 7 12 17 22
> 3 8 13 18 23
> 4 9 14 19 24
>
> The Octave documentation states that indexing multidimensional arrays when indexed by a scalar are treated in column-major order. Hence in the above example because of the way I defined i, a(i) is transposed.
>
> https://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html
>
> Dan
>
Anyone checked Matlab compatibility yet?