help-octave
[Top][All Lists]

Re: Cell arrays of strings in "sort" and "unique"

 From: David Bateman Subject: Re: Cell arrays of strings in "sort" and "unique" Date: Tue, 14 Sep 2004 10:00:15 +0200 User-agent: Mutt/1.4.1i

```It seems to me that if sort('bcda') works in matlab, it should as well
in octave. So see the attached patch.

As for sorting of cell arrays, matlab handles this by an overloaded
function. We should probably do the same thing, at this point as
part of octave-forge since dispatch is there. But when classes are
in octave, as part of octave itself...

Regards

* DLD-FUNCTIONS/sort.cc (static octave_value_list
mx_sort (charNDArray &m, bool return_idx, int dim)): New function
to sort strings
(Fsort): adapt to allow strings to be passed

According to Keith Goodman <address@hidden> (on 09/14/04):
> Sorry for the one-way conversation. Since sortrows supports sorting of
> strings it is straightforward to convert unique to support cell arrays
> of strings:
>
> function [y,i] = unique_cell(x)
> n = size(x,1);
> y = char(x);
> [y,i] = sortrows(y);
> match = all(y(1:n-1,:)==y(2:n,:),2);
> i(find(match)) = [];
> y = x(i,:);
>
>
>
> ---------- Forwarded message ----------
> Date: Mon, 13 Sep 2004 17:08:31 -0700
> Subject: RE: Cell arrays of strings in "sort" and "unique"
>
> The code below, unique_cell, doesn't work
>
> 1. The minus sign should be moved one line down (-y)
> 2. I just updated to 2.1.58 which gives a different result for
> num2str([1 2; 3 4]) than 2.1.57
> 3. My strings are short; if yours are long then the numbers get too
> big which means characters at the end will be ignored.
> 4. ASCII code like 12 3 45 is incorrectly assumed to be equal to 123 4 5.
>
> In other words, does anyone know a quick (minimal looping) routine for
> a unique on cell arrays on strings?
>
>
>
> ---------- Forwarded message ----------
> Date: Mon, 13 Sep 2004 15:19:38 -0700
> Subject: Cell arrays of strings in "sort" and "unique"
>
> I noticed that the functions "sort and "unique" do not support cell
> arrays of strings. And that sort does not support strings.
>
> As a work around for unique, I'm using
>
> function [z,i] = unique_cell(x)
> x = x(:);
> y = str2num(num2str(-1*char(x)));
> [ignore,i] = unique(y);
> z = x(i,:);
>
> Could the above technique of converting a cell array to a char array
> to a number matrix to a string array to a number array be used in sort
> and unique to handle cell arrays of strings?
>
>
>
> -------------------------------------------------------------
> 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
> -------------------------------------------------------------

--
Motorola CRM                                 +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: