Thread Subject: Unique but not wanting to reorder!

Subject: Unique but not wanting to reorder!

From: Nicola

Date: 20 Oct, 2009 20:00:18

Message: 1 of 10

Hey guys,

so far i have the programme:

function Ciphertext = keywordcipher(Plaintext, Keyword);

key = lower(Keyword);
key(isspace(key))= '';

Values = double(key);

Order = unique(Values)



Then in the command window i have:

EDU>> keywordcipher('Hello','EDCBA')

Order =

    97 98 99 100 101


But im not wanting my values to reorder themselves, i want them to stay in that order! so it'd be 101, 100, 99, 98, 97. Is there a way that i can do unique without them reordering themselves?

Many thanks!

Subject: Unique but not wanting to reorder!

From: Matt

Date: 20 Oct, 2009 20:16:02

Message: 2 of 10

"Nicola " <x_nicolabell_x@yahoo.co.uk> wrote in message <hbl4si$a53$1@fred.mathworks.com>...
> Hey guys,
>
> so far i have the programme:
>
> function Ciphertext = keywordcipher(Plaintext, Keyword);
>
> key = lower(Keyword);
> key(isspace(key))= '';
>
> Values = double(key);
>
> Order = unique(Values)
>
>
>
> Then in the command window i have:
>
> EDU>> keywordcipher('Hello','EDCBA')
>
> Order =
>
> 97 98 99 100 101
>
>
> But im not wanting my values to reorder themselves, i want them to stay in that order! so it'd be 101, 100, 99, 98, 97. Is there a way that i can do unique without them reordering themselves?
=======


No, because the ordering that you propose is ill-defined.

For example, suppose Values=[99 98 99]

Then unique(Values) will contain 98 and 99, but both the sequence [98 99] and
[99, 98] are present in Values, so which one would you consider the "un-reordered" version? Both are equally valid candidates.

Subject: Unique but not wanting to reorder!

From: someone

Date: 20 Oct, 2009 20:26:02

Message: 3 of 10

"Nicola " <x_nicolabell_x@yahoo.co.uk> wrote in message <hbl4si$a53$1@fred.mathworks.com>...
> Hey guys,
>
> so far i have the programme:
>
> function Ciphertext = keywordcipher(Plaintext, Keyword);
>
> key = lower(Keyword);
> key(isspace(key))= '';
>
> Values = double(key);
>
> Order = unique(Values)
>
>
>
> Then in the command window i have:
>
> EDU>> keywordcipher('Hello','EDCBA')
>
> Order =
>
> 97 98 99 100 101
>
>
> But im not wanting my values to reorder themselves, i want them to stay in that order! so it'd be 101, 100, 99, 98, 97. Is there a way that i can do unique without them reordering themselves?
>
> Many thanks!

% Try replacing:

Order = unique(Values)

% with

[Order,indx] = unique(Values)
Order = Values(sort(index))

Subject: Unique but not wanting to reorder!

From: Michael Hosea

Date: 20 Oct, 2009 20:29:22

Message: 4 of 10

The algorithm used in UNIQUE relies on sorting because it is faster for
large sets. However, there is sufficient information returned in the second
argument to recover the original ordering. You can do this in two steps:

    [~,idx] = unique(Values,'first');
    Order = Values(sort(idx));

--
Mike



"Nicola " <x_nicolabell_x@yahoo.co.uk> wrote in message
news:hbl4si$a53$1@fred.mathworks.com...
> Hey guys,
>
> so far i have the programme:
>
> function Ciphertext = keywordcipher(Plaintext, Keyword);
>
> key = lower(Keyword);
> key(isspace(key))= '';
>
> Values = double(key);
>
> Order = unique(Values)
>
>
>
> Then in the command window i have:
>
> EDU>> keywordcipher('Hello','EDCBA')
>
> Order =
>
> 97 98 99 100 101
>
>
> But im not wanting my values to reorder themselves, i want them to stay in
> that order! so it'd be 101, 100, 99, 98, 97. Is there a way that i can do
> unique without them reordering themselves?
>
> Many thanks!

Subject: Unique but not wanting to reorder!

From: someone

Date: 20 Oct, 2009 20:33:03

Message: 5 of 10

"someone" <someone@somewhere.net> wrote in message <hbl6cq$jtl$1@fred.mathworks.com>...
> "Nicola " <x_nicolabell_x@yahoo.co.uk> wrote in message <hbl4si$a53$1@fred.mathworks.com>...
> > Hey guys,
> >
> > so far i have the programme:
> >
> > function Ciphertext = keywordcipher(Plaintext, Keyword);
> >
> > key = lower(Keyword);
> > key(isspace(key))= '';
> >
> > Values = double(key);
> >
> > Order = unique(Values)
> >
> >
> >
> > Then in the command window i have:
> >
> > EDU>> keywordcipher('Hello','EDCBA')
> >
> > Order =
> >
> > 97 98 99 100 101
> >
> >
> > But im not wanting my values to reorder themselves, i want them to stay in that order! so it'd be 101, 100, 99, 98, 97. Is there a way that i can do unique without them reordering themselves?
> >
> > Many thanks!
>
> % Try replacing:
>
> Order = unique(Values)
>
> % with
>
> [Order,indx] = unique(Values)
> Order = Values(sort(index))

% Matt still has a valid point.
% You could also add the 'first' or 'last' option to help resolve this.
% See:
doc unique
% But you stll would have the same ambiguity
% if there are more han two identical values.

Subject: Unique but not wanting to reorder!

From: Nicola

Date: 20 Oct, 2009 20:50:18

Message: 6 of 10

THANKS EVERYONE!!!!!




"Nicola " <x_nicolabell_x@yahoo.co.uk> wrote in message <hbl4si$a53$1@fred.mathworks.com>...
> Hey guys,
>
> so far i have the programme:
>
> function Ciphertext = keywordcipher(Plaintext, Keyword);
>
> key = lower(Keyword);
> key(isspace(key))= '';
>
> Values = double(key);
>
> Order = unique(Values)
>
>
>
> Then in the command window i have:
>
> EDU>> keywordcipher('Hello','EDCBA')
>
> Order =
>
> 97 98 99 100 101
>
>
> But im not wanting my values to reorder themselves, i want them to stay in that order! so it'd be 101, 100, 99, 98, 97. Is there a way that i can do unique without them reordering themselves?
>
> Many thanks!

Subject: Unique but not wanting to reorder!

From: Michael Hosea

Date: 20 Oct, 2009 20:59:13

Message: 7 of 10

"someone" <someone@somewhere.net> wrote in message
news:hbl6pv$hgu$1@fred.mathworks.com...
> % Matt still has a valid point.

Yes, for sure. I only guessed that the OP would want the results ordered
according to first appearance.
--
Mike

Subject: Unique but not wanting to reorder!

From: Jan Simon

Date: 20 Oct, 2009 21:40:17

Message: 8 of 10

Dear Nicola!

> Order = unique(Values)
> 97 98 99 100 101
>
> But im not wanting my values to reorder themselves, i want them to stay in that order! so it'd be 101, 100, 99, 98, 97. Is there a way that i can do unique without them reordering themselves?

A simplified UNIQUE including reconstruction of original order:

function Y = unique_no_reordering(X)
[Xs, SortVec] = sort(X(:));
UV(SortVec) = ([1; diff(Xs)] ~= 0);
Y = X(UV);
return;

Perhaps this helps to save some processing time. Jan

Subject: Unique but not wanting to reorder!

From: Bruno Luong

Date: 20 Oct, 2009 22:05:18

Message: 9 of 10

[u i]=unique(A,'first');
A(sort(i))

% Bruno

Subject: Unique but not wanting to reorder!

From: Jan Simon

Date: 21 Oct, 2009 08:11:18

Message: 10 of 10

Dear Nicola!

> Bruno wrote:
> [u i]=unique(A,'first');
> A(sort(i))

That's it. My method is faster because it contains just the needed commands cut out from the original UNIQUE, but avoid the 2nd SORT. Nevertheless, the prompt "EDU >>" in the question implies, that you are student and the processing time is most likely not your most important problem. Calling functions of the Matlab toolbox is recommended in this case, because the implementation of functions optimized for processing time result in a waste of time for debugging!

Thanks Bruno, Jan

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com