octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sort indexing


From: Daniel J Sebald
Subject: Re: sort indexing
Date: Mon, 15 Feb 2016 19:11:51 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Thunderbird/3.1.16

On 02/15/2016 06:43 PM, Nicholas Jankowski wrote:

On Feb 15, 2016 4:10 PM, "Daniel J Sebald" <address@hidden
<mailto: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?

If there is no such thing, we could add a mode "scalar" to both sort() and sortrows(), e.g., rather than

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] = sort(a)
i =

   1   1   1   1   1
   2   2   2   2   2
   3   3   3   3   3
   4   4   4   4   4
   5   5   5   5   5

the user could use

octave:2> [~,i] = sort(a, "scalar")
i =

   1   6  11  16  21
   2   7  12  17  22
   3   8  13  18  23
   4   9  14  19  24
   5  10  15  20  25

in which case a(i) would then equal b.

Dan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]