Asked by Staf
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

*No products are associated with this question.*

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)

Show 2 older comments

Walter Roberson
on 22 May 2012

Opportunities for recent engineering grads.

## 5 Comments

## Thomas (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38955#comment_80571

Can you give an example of what your output should be..

## Staf (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38955#comment_80573

Hello, Thomas

I have the following table in excel

time country Location price2

1/09 Austria l1 12

2/09 Austria l1 4

3/09 Austria l1 2

1/09 Austria l2 1

2/09 Austria l2 23

3/09 Austria l2 14

1/09 Germany l1 100

2/09 Germany l1 110

3/09 Germany l1 120

1/09 Germany l2 100

2/09 Germany l2 110

3/09 Germany l2 120

I want to obtain the following structure

1/09 Austria l1 12

1/09 Germany l1 100

2/09 Austria l1 4o

2/09 Germany l1 1 10

3/09 Austria l1 2

3/09 Germany l1 120

1/09 Austria l2 1

1/09 Germany l2 100

2/09 Austria l2 23

2/09 Germany l2 110

and so forth...

the first 3 varibles are string varibles.

As you can see I want to gather together all l1s that correspond to 1/09

then, i want to gather all l1s that correspond to 2/09

then, i want to gather all l1s that correspond to 3/09

then, i want to gather all l2s that correspond to `1/09

then, i want to gather all l2s that correspond to `2/09

then, i want to gather all l2s that correspond to `3/09

and so forth.....

## Oleg Komarov (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38955#comment_80577

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

## Staf (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38955#comment_80587

yes, thank you again

## Oleg Komarov (view profile)

Direct link to this comment:http://nl.mathworks.com/matlabcentral/answers/38955#comment_80716

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.