Converting first column unique values to number

2 ビュー (過去 30 日間)
Bewler
Bewler 2019 年 6 月 28 日
コメント済み: Bewler 2019 年 7 月 1 日
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 件のコメント
dpb
dpb 2019 年 6 月 28 日
Keeping the same number or just 1:NoUnique values?
Bewler
Bewler 2019 年 6 月 28 日
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.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2019 年 6 月 28 日
編集済み: Matt J 2019 年 6 月 28 日
[~,~,u]=unique(yourTable{:,1},'stable');
yourTable{:,1}=num2cell(u)
  4 件のコメント
Matt J
Matt J 2019 年 6 月 29 日
編集済み: Matt J 2019 年 6 月 29 日
@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 2019 年 7 月 1 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOther Formats についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by