MATLAB Answers

Stef

converting a string of variables to numbers

Asked by Stef
on 21 May 2012

HI all I have the varible

Country
Austria
Austria
Austria
Austria
Austria
Austria
Austria
 and I want to convert it to a numeric varable. Any help?

Thanks in advance

  5 Comments

Oleg Komarov
on 21 May 2012

To be more precise you're grouping by location AND country AND date.

Stef
on 21 May 2012

yes, thank you again

Oleg Komarov
on 21 May 2012

Now that I read your duplicated question: http://www.mathworks.com/matlabcentral/answers/38910-grouping-observations-with-specific-characteristics

I understand that you do NOT want to gather but sort.
Geoff already answered your question, you have to use sortrows.

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 21 May 2012
 Accepted answer

Use the multi-output form of unique() to convert string variables to numeric indices.

When you have the numeric indices, use accumarray() to sum the data into a 3D table (date, country, I-value).

Either compute further things with that table or output it, skipping the 0 entries (the ones for which there was no input data)

  5 Comments

Walter Roberson
on 21 May 2012

Easier:

[uniqueNames, junk, nameID] = unique(names);

Walter Roberson
on 22 May 2012

Note: this solution is for adding together all of the values associated with a particular triple of attributes. If you only have one value for any given combination, then this approach is not appropriate, but sortrows() would be appropriate.

Stef
on 22 May 2012

Tnank you both!


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today