user1737564
user1737564

Reputation: 53

How do I change cell array to numeric values in MATLAB?

I want to the do the following

Input: (cell array)

ab

ac

ad

aab

ac

aac

aab

ac

I want the output to map to unique numeric values, like

1

2

3

4

2

5

4

2

Is there an easy way to do this? The input is about 250,000 and of variable length. I just want to map the cells with the same content to the same number.

Thanks.

Upvotes: 1

Views: 166

Answers (1)

Floris
Floris

Reputation: 46375

If we call your cell array A, then the following command does what you need:

[uniqueCells,~,idxYouWant] = unique(A);

In this, the uniqueCells are the unique values you have (in sorted order); and idxYouWant is an array of numbers like you want, where

A = uniqueCells(idxYouWant);

I think this is exactly what you need.

Upvotes: 5

Related Questions