Converting first column unique values to number

I have a table that I need to use for another function.
Currently the table is setup as
Base1,x,y,z
Base1,x,y,z
Building2,x,y,z
Building2,x,y,z
Complex3,x,y,z
Complex3,x,y,z
Complex3,x,y,z
What I would like to do is rename the first column if they have unique names so Base1 would switch to 1, Building2 switches to 2 ect...
Is there an easy way to rename all of the first column based on if the values are unique or not?

2 commentaires

dpb
dpb le 28 Juin 2019
Keeping the same number or just 1:NoUnique values?
Bewler
Bewler le 28 Juin 2019
There isn't normally a number in the first colum, its actually A,B,C. I just want to convert any similar values in column 1 to numbers starting at 1:n.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 28 Juin 2019
Modifié(e) : Matt J le 28 Juin 2019
[~,~,u]=unique(yourTable{:,1},'stable');
yourTable{:,1}=num2cell(u)

4 commentaires

Bewler
Bewler le 28 Juin 2019
Matt, this converted the first column to a cell. I can't seem to convert this back to a double...
The numbers are correct 1-3 however I can't use the numbers now for my next function and it seems you can't convert these back. I tried
yourTable(:,1) = cell2mat(yourTable{:,1}) however it says the right hand side of an assignment into a table must be another table or a cell array.
Stephen23
Stephen23 le 29 Juin 2019
Rather than converting the double array to a cell array of scalar numerics and then converting that back to a duoble array, just don't use num2cell in the first place.
Matt J
Matt J le 29 Juin 2019
Modifié(e) : Matt J le 29 Juin 2019
@Bewler,
To replace the column with variables of a different type, you need to use dot-indexing,
[~,~,u]=unique(yourTable{:,1},'stable');
yourTable.Var1=u;
where 'Var1' is the variable name for the column in the table.
Bewler
Bewler le 1 Juil 2019
Stephen. The issue is we have to convert due to the original input of Var1 of the table as a cell and we can't directly overwrite the variable with a double. I'm not familiar with dot-indexing but I'll explore this as well. Not too familiar with tables and why we can't easily overwrite the cell as a double.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by