From: Fraser Dickson on
Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.

I have a 2d array always with 2 cols

I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1

eg.

a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]

i want b to equal what i have mentioned above which should hopefully give me

b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]

a = unique(b) does not do this ?

Can someone help me out ?
From: Wayne King on
"Fraser Dickson" <fraser.dickson(a)gmail.com> wrote in message <hrrgbg$mk0$1(a)fred.mathworks.com>...
> Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.
>
> I have a 2d array always with 2 cols
>
> I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1
>
> eg.
>
> a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]
>
> i want b to equal what i have mentioned above which should hopefully give me
>
> b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]
>
> a = unique(b) does not do this ?
>
> Can someone help me out ?

Hi, One way

[elements,I,J] = unique(a(:,2),'first');
B = zeros(size(a));
B(I,:)=a(I,:);
EmptyInd = setdiff(1:size(a,1),I);
B(EmptyInd,:)=[];

Wayne
From: Roger Stafford on
"Fraser Dickson" <fraser.dickson(a)gmail.com> wrote in message <hrrgbg$mk0$1(a)fred.mathworks.com>...
> Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.
>
> I have a 2d array always with 2 cols
>
> I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1
>
> eg.
>
> a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]
>
> i want b to equal what i have mentioned above which should hopefully give me
>
> b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]
>
> a = unique(b) does not do this ?
>
> Can someone help me out ?
- - - - - - - -
It depends on what you want, Fraser. The unique function puts the unique elements in ascending order. We can't tell from your example if this is actually what you want because the first occurrence of numbers in the second column there happens to already be in ascending order.

If you want to leave them in ascending order do this

[t,m,n] = unique(a(:,2),'first');
b = a(m,:);

On the other hand if you want to preserve the original order that was present in a, do this

[t,m,n] = unique(a(:,2),'first');
[t,p] = sort(m);
b = a(m(p),:);

Roger Stafford
From: us on
"Fraser Dickson" <fraser.dickson(a)gmail.com> wrote in message <hrrgbg$mk0$1(a)fred.mathworks.com>...
> Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.
>
> I have a 2d array always with 2 cols
>
> I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1
>
> eg.
>
> a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]
>
> i want b to equal what i have mentioned above which should hopefully give me
>
> b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]
>
> a = unique(b) does not do this ?
>
> Can someone help me out ?

one of the many other solutions

a=[
2 23
3 25
4 23
5 25
6 26
7 23
8 27
9 28
10 29
];
[ax,ax,ax]=unique(a(:,2));
[ax,ax]=unique(ax,'first');
r=a(ax,:)
%{
2 23
3 25
6 26
8 27
9 28
10 29
%}

us
From: Bruno Luong on
"us " <us(a)neurol.unizh.ch> wrote in message <hrsba0$bj3$1(a)fred.mathworks.com>...
>

> [ax,ax,ax]=unique(a(:,2));
> [ax,ax]=unique(ax,'first');
> r=a(ax,:)

Hm, Unless if I'm mistaken I can't see why the advantage above to this:

[ax,ax]=unique(a(:,2),'first');
r=a(ax,:)

otherwise than heating twice the CPU and and being really obscure. Can we be enlighten us? I admittedly staring the three lines of code for a moment... LOL

Bruno