Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Unique elements of a structure

Subject: Unique elements of a structure

From: Liana

Date: 27 Oct, 2011 19:58:30

Message: 1 of 3

Hello,

I need to find unique elements of a structure:
elems = struct('nums', {})
elems(1).nums = [1 2 3];
elems(2).nums = [1 2 3 4];
elems(3).nums = [1 2 3];

The result should be:
elems(1).nums = [1 2 3];
elems(2).nums = [1 2 3 4];

It seems that the function 'unique' does not work for this case. Also, I would not like to use 'for' loop. Is there any other way to get unique elements?

Thanks.

Subject: Unique elements of a structure

From: Matt J

Date: 27 Oct, 2011 21:19:29

Message: 2 of 3

"Liana" wrote in message <j8cd55$ds9$1@newscl01ah.mathworks.com>...
> Hello,
>
> I need to find unique elements of a structure:
> elems = struct('nums', {})
> elems(1).nums = [1 2 3];
> elems(2).nums = [1 2 3 4];
> elems(3).nums = [1 2 3];
>
> The result should be:
> elems(1).nums = [1 2 3];
> elems(2).nums = [1 2 3 4];
>
> It seems that the function 'unique' does not work for this case. Also, I would not like to use 'for' loop. Is there any other way to get unique elements?

It depends on the range of values that your nums can assume. For your example the following works

C=cellfun(@char,{elems.nums},'unif',0);
[~,idx]=unique(C);
uniqueElems=elems(idx),

Subject: Unique elements of a structure

From: Liana

Date: 28 Oct, 2011 19:48:29

Message: 3 of 3

Thanks.

"Matt J" wrote in message <j8cht1$pc$1@newscl01ah.mathworks.com>...
> "Liana" wrote in message <j8cd55$ds9$1@newscl01ah.mathworks.com>...
> > Hello,
> >
> > I need to find unique elements of a structure:
> > elems = struct('nums', {})
> > elems(1).nums = [1 2 3];
> > elems(2).nums = [1 2 3 4];
> > elems(3).nums = [1 2 3];
> >
> > The result should be:
> > elems(1).nums = [1 2 3];
> > elems(2).nums = [1 2 3 4];
> >
> > It seems that the function 'unique' does not work for this case. Also, I would not like to use 'for' loop. Is there any other way to get unique elements?
>
> It depends on the range of values that your nums can assume. For your example the following works
>
> C=cellfun(@char,{elems.nums},'unif',0);
> [~,idx]=unique(C);
> uniqueElems=elems(idx),

Tags for this Thread

No tags are associated with this thread.

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.

Contact us