[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Smarter Sort
From: |
David Bateman |
Subject: |
Re: Smarter Sort |
Date: |
Thu, 25 Aug 2005 16:07:35 +0200 |
User-agent: |
Mozilla Thunderbird 0.8 (X11/20040923) |
Bill Denney wrote:
I'm wanting to sort cell arrays of chars in a smart way. Essentially,
I'd like to do something like:
A3B10
A3B9
A10
A1
A2
becomes
A1
A2
A3B9
A3B10
A10
so that it will sort the numeric portions intelligently. Does anyone
have a routine that will do that or is there a built-in sort that I
don't know of?
Thanks,
Bill
This is not a typical way to sort the above... For example sorting cell
arrays gives
octave:1> a{1} = "A3B10"; a{2} = "A3B9"; a{3} = "A10"; a{4} = "A1"; a{5}
= "A2";
octave:2> a
a =
{
[1,1] = A3B10
[1,2] = A3B9
[1,3] = A10
[1,4] = A1
[1,5] = A2
}
octave:3> sort(a)
ans =
{
[1,1] = A1
[1,2] = A10
[1,3] = A2
[1,4] = A3B10
[1,5] = A3B9
}
which respects the alphabetical order. Sorting of the first numerical
field as you've done would mean that you would have to extract this
numerical field in some manner. If these are indexes in to a table with
index A and B, why not store this as
A(1) = 3; A(2) = 3; A(3) = 10; A(4) = 1; A(5) = 2;
B(1) = 10; B(2) = 9; B(3) = 0; B(4) = 0; B(5) = 0;
[val, idx] = sort (A);
[dummy, idx2] = unique (val);
i = find(diff(idx2) > 1);
idx1 = idx2(i)+1;
idx2 = idx2(i+1);
for i = 1:length(idx1)
[dummy, idx(idx1(i):idx2(i))] = sort (A(idx(idx1(i):idx2(i))));
endfor
[A(idx)', B(idx)']
You could probably even get rid of the for-loop if you tried a little
harder..
Cheers
David
idx2 = find(diff(val) == 0.0);
--
David Bateman address@hidden
Motorola Labs - Paris +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE
The information contained in this communication has been classified as:
[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------