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:
how to use unique on a column of numbers

Subject: how to use unique on a column of numbers

From: Alan Hitch

Date: 6 Mar, 2014 02:08:08

Message: 1 of 5

The beginning of my data is a cell array as follows:
'Point' 'Date' 'Time' 'TimeInterval' 'Species'
1 '5/14/2005' '0-4' 'A' 'DOWO'
1 '5/14/2005' '0-4' 'A' 'LOWA'
1 '5/14/2005' '0-4' 'A' 'ACFL'
1 '5/14/2005' '0-4' 'A' 'ACFL'
1 '5/14/2005' '5-8' 'B' 'WOTH'
1 '5/14/2005' '5-8' 'B' 'WEWA'
1 '5/14/2005' '5-8' 'B' 'BGGN'
1 '5/14/2005' '5-8' 'B' 'LOWA'

There are 10000 records with multiple points. I would like to use unique to identify the unique points but I keep getting the error: must be a cell array of strings. I would like to use the Point column because when I output the results I need to sort in numerical order. Is there any way I can change the column to strings use unique and then change it back?

Thanks
Alan

Subject: how to use unique on a column of numbers

From: Phil Goddard

Date: 6 Mar, 2014 04:54:10

Message: 2 of 5

>> uniquePoints = unique(cell2mat(yourData(2:end,1)));

Phil.

Subject: how to use unique on a column of numbers

From: Steven Lord

Date: 6 Mar, 2014 14:57:24

Message: 3 of 5


"Alan Hitch" <athitch@ucdavis.edu> wrote in message
news:lf8la8$phg$1@newscl01ah.mathworks.com...
> The beginning of my data is a cell array as follows:
> 'Point' 'Date' 'Time' 'TimeInterval' 'Species'
> 1 '5/14/2005' '0-4' 'A' 'DOWO'
> 1 '5/14/2005' '0-4' 'A' 'LOWA'
> 1 '5/14/2005' '0-4' 'A' 'ACFL'
> 1 '5/14/2005' '0-4' 'A' 'ACFL'
> 1 '5/14/2005' '5-8' 'B' 'WOTH'
> 1 '5/14/2005' '5-8' 'B' 'WEWA'
> 1 '5/14/2005' '5-8' 'B' 'BGGN'
> 1 '5/14/2005' '5-8' 'B' 'LOWA'
>
> There are 10000 records with multiple points. I would like to use unique
> to identify the unique points but I keep getting the error: must be a cell
> array of strings. I would like to use the Point column because when I
> output the results I need to sort in numerical order. Is there any way I
> can change the column to strings use unique and then change it back?

UNIQUE returns not only the unique elements of the array, but also two index
arrays (ia and ic) that you can use to generate the output from the input or
vice versa using indexing.

If you want the value in Point to be taken into consideration when deciding
whether a row is unique, you will need to convert it to a string. The
easiest way is to use CHAR (if all the values are "sufficiently small"
nonnegative integer values) or NUM2STR (if they're not.)

From the organization of your data, you may also be interested in storing it
not as a cell array but as a table, possibly with categories represented as
categorical variables.

http://www.mathworks.com/help/matlab/ref/table.html

http://www.mathworks.com/help/matlab/ref/categorical.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: how to use unique on a column of numbers

From: Alan Hitch

Date: 6 Mar, 2014 19:59:10

Message: 4 of 5

"Phil Goddard" <phil@goddardconsulting.ca> wrote in message <lf8v1i$hm5$1@newscl01ah.mathworks.com>...
> >> uniquePoints = unique(cell2mat(yourData(2:end,1)));
>
> Phil.

Thanks Phil. This worked great.

Subject: how to use unique on a column of numbers

From: Alan Hitch

Date: 6 Mar, 2014 20:02:10

Message: 5 of 5

Thanks Steven. I am going to start experimenting with tables.

"Steven Lord" <Steven_Lord@mathworks.com> wrote in message <lfa2ce$caj$1@newscl01ah.mathworks.com>...
>
> "Alan Hitch" <athitch@ucdavis.edu> wrote in message
> news:lf8la8$phg$1@newscl01ah.mathworks.com...
> > The beginning of my data is a cell array as follows:
> > 'Point' 'Date' 'Time' 'TimeInterval' 'Species'
> > 1 '5/14/2005' '0-4' 'A' 'DOWO'
> > 1 '5/14/2005' '0-4' 'A' 'LOWA'
> > 1 '5/14/2005' '0-4' 'A' 'ACFL'
> > 1 '5/14/2005' '0-4' 'A' 'ACFL'
> > 1 '5/14/2005' '5-8' 'B' 'WOTH'
> > 1 '5/14/2005' '5-8' 'B' 'WEWA'
> > 1 '5/14/2005' '5-8' 'B' 'BGGN'
> > 1 '5/14/2005' '5-8' 'B' 'LOWA'
> >
> > There are 10000 records with multiple points. I would like to use unique
> > to identify the unique points but I keep getting the error: must be a cell
> > array of strings. I would like to use the Point column because when I
> > output the results I need to sort in numerical order. Is there any way I
> > can change the column to strings use unique and then change it back?
>
> UNIQUE returns not only the unique elements of the array, but also two index
> arrays (ia and ic) that you can use to generate the output from the input or
> vice versa using indexing.
>
> If you want the value in Point to be taken into consideration when deciding
> whether a row is unique, you will need to convert it to a string. The
> easiest way is to use CHAR (if all the values are "sufficiently small"
> nonnegative integer values) or NUM2STR (if they're not.)
>
> From the organization of your data, you may also be interested in storing it
> not as a cell array but as a table, possibly with categories represented as
> categorical variables.
>
> http://www.mathworks.com/help/matlab/ref/table.html
>
> http://www.mathworks.com/help/matlab/ref/categorical.html
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Tags for 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