help-octave
[Top][All Lists]
Advanced

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

index returned by sort (possible modification ind2sub, sub2ind)


From: PetrSt
Subject: index returned by sort (possible modification ind2sub, sub2ind)
Date: Tue, 18 Jun 2013 02:36:23 -0700 (PDT)

Let's have nd-array y of size ysiz = [n(1),...,n(nd)] = size(y), nd=ndims(y).
I want ot sort y across some specified dimension D (1<=D<=nd), but it cann't
be done directly by sort, because y could be, for example, a structure and
sort should be done by some more complex evaluation of its fields.
However, I'm able to construct function "fun" so that x=fun(y) and returned
x is float matrix of the same size as y (xsiz = ysiz). This x can be easily
sorted by [x_sorted, ind_xs] = sort(x,D).
Unfortunately y_sorted = y(ind_xs) is not applicable, because ind_xs is
matrix of the same size as x (and also y), but its values are not global
linear indices. Its elements are indices across the dimension D, i.e.
1<=ind_xs<=n(D).
The only way I see is to involve ind2sub and sub2ind functions like 
   [idx(1),...,idx(nd)] = ind2sub(xsiz,(1:prod(xsiz))');
   idx(:,D) = ind_xs(:);
   ind = sub2ind(xsiz,idx(1),...,idx(nd));
   ind = reshape(ind,xsiz);
   y_sorted = y(ind);
Maybe there is a better way?

Another problem arises from the above one. If x is for example passed
argument, its size is obtainable by size command, but it's unknown at the
moment of code generation, i.e. number of dimensions nd could be variable.
Then the syntax of ind2sub and sub2ind is a bit uncomfortable, since it has
to be evaluated as a text command by eval (see example in file testik2.m).
I'm using precompiled instalation for win, therefore I cann't see the code
of these functions and I don't know how hard it would be to modify them to
accept array as input (sub2ind) or return an array (in2sub). I tried to do
it myself ( subtoind.m
<http://octave.1599824.n4.nabble.com/file/n4654406/subtoind.m>   and 
indtosub.m <http://octave.1599824.n4.nabble.com/file/n4654406/indtosub.m>  )
as a suggestion for modification.

Simplified example of both problems is given in file  testik2.m
<http://octave.1599824.n4.nabble.com/file/n4654406/testik2.m>   .

PS: During writting of this question I recognized that ind2sub and sub2ind
works well with the cells (see testik2.m - example with cell notation) and
thus no modification is needed. It could be a good idea to mention it in
documentation to these functions. Nevertheless, my question about efficient
sorting of an array in accordance with the sorting of another array of the
same size still holds.

Regards
Petr



--
View this message in context: 
http://octave.1599824.n4.nabble.com/index-returned-by-sort-possible-modification-ind2sub-sub2ind-tp4654406.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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