octave-maintainers
[Top][All Lists]
Advanced

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

Re: sort indexing


From: John W. Eaton
Subject: Re: sort indexing
Date: Mon, 15 Feb 2016 20:47:02 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.5.0

On 02/15/2016 03: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)

In your example, the index I is a matrix, but it's just the sort order for each individual column. When indexing an array (not a vector) with a single argument, Octave uses it as a linear index into the array and returns a result with the same shape as the index. It works this way because that's what Matlab does so we can't change it to mean something else.

jwe




reply via email to

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