From: anna on
Hi,
I want to supress repetition in an array but I don't want to use unique function, I wrote this piece of code, but I think that there is a better way to optimize it and use much less loops:
"b=[0.23 0.78 0.45 0.23 0.89 0.45 0.78]
for i=1:length(b)-1
for j=(i+1):length(b)
if b(i)== b(j)
b([j])=50;
end
end
end
a=1;
k=1;
for k=1:length(b)
if b(k)~= 50
t(a)=b(k);
a=a+1;
k=k+1;
else
a=a;
k=k+1;
end
end
"
any idea?
Thanks
From: Rune Allnor on
On 27 Des, 18:24, "anna " <ghada_za...(a)hotmail.fr> wrote:
> Hi,
> I want to supress repetition in an array but I don't want to use unique function, I wrote this piece of code, but I think that there is a better way to optimize it and use much less loops:

There are several ways to do this, depending on
what constraints are imposed on the result.
If you are allowed to shuffle the elements
at will, the simplest way is to sort the data
and then remove duplicates. If you need to
preserve the order of the remaining elements,
the job becomes a little more cumbersome, but
not much.

Rune
From: anna on
Rune Allnor <allnor(a)tele.ntnu.no> wrote in message <2a01450a-e217-4e1c-977d-9f5e4c245f8f(a)b2g2000yqi.googlegroups.com>...
> On 27 Des, 18:24, "anna " <ghada_za...(a)hotmail.fr> wrote:
> > Hi,
> > I want to supress repetition in an array but I don't want to use unique function, I wrote this piece of code, but I think that there is a better way to optimize it and use much less loops:
>
> There are several ways to do this, depending on
> what constraints are imposed on the result.
> If you are allowed to shuffle the elements
> at will, the simplest way is to sort the data
> and then remove duplicates. If you need to
> preserve the order of the remaining elements,
> the job becomes a little more cumbersome, but
> not much.
>
> Rune
Thanks Rune
I want to prserve the order of the elements so I can't use sort
From: Rune Allnor on
On 27 Des, 20:10, "anna " <ghada_za...(a)hotmail.fr> wrote:
> Rune Allnor <all...(a)tele.ntnu.no> wrote in message <2a01450a-e217-4e1c-977d-9f5e4c245...(a)b2g2000yqi.googlegroups.com>...
> > On 27 Des, 18:24, "anna " <ghada_za...(a)hotmail.fr> wrote:
> > > Hi,
> > > I want to supress repetition in an array but I don't want to use unique function, I wrote this piece of code, but I think that there is a better way to optimize it and use much less loops:
>
> > There are several ways to do this, depending on
> > what constraints are imposed on the result.
> > If you are allowed to shuffle the elements
> > at will, the simplest way is to sort the data
> > and then remove duplicates. If you need to
> > preserve the order of the remaining elements,
> > the job becomes a little more cumbersome, but
> > not much.
>
> > Rune
>
> Thanks Rune
> I want to prserve the order of the elements so I can't use sort

Yes you can. You just have to keep track of where
the sorted elements are located in the original
sequence. You can do that by using the 2nd output
from matlab's SORT function. That way you can tag
the relevant elements in the original sequence as
duplicates, and delete them at the end.

Rune
From: anna on

>
> Yes you can. You just have to keep track of where
> the sorted elements are located in the original
> sequence. You can do that by using the 2nd output
> from matlab's SORT function. That way you can tag
> the relevant elements in the original sequence as
> duplicates, and delete them at the end.
>
> Rune
Thanks rune , so there is no way to simplify the code, unless the use of sort function?