From: Oleg Komarov on
"anna " <ghada_zaibi(a)hotmail.fr> wrote in message <hhco81$l3p$1(a)fred.mathworks.com>...
>
> >
> > 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?

You sort it, then use diff, then index the zeros and eliminate the values and the indexes corresponding to the locations of zeros. Then resort the restricted vector back.

Oleg
From: Andy on
"anna " <ghada_zaibi(a)hotmail.fr> wrote in message <hh857j$4gn$1(a)fred.mathworks.com>...
> 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

If you're just looking to simplify this code, then remove the second loop entirely. It makes no sense. You're editing the for loop indexing variable k within the loop (which is sure to have unintended consequences), and you're doing the whole loop to check for a specific flag value that you set in the first loop. You can replace the entire second loop with something like b(b==50)=[];

But you should look into the sorting methods. If b is a vector of length n, then your first loop will take roughly n^2 operations to flag the duplicated values. But the sorting method will take roughly n log n operations, a significant improvement. And the code will be much cleaner.