How to convert strings in a table to numbers?

조회 수: 7(최근 30일)
Tara
Tara 2022년 8월 2일
댓글: Tara 2022년 8월 2일

I have a very large table (611507 x 7) where some columns are strings and need to be converted to numbers.
On the other hand, there are repeated strings in the same columns, which should have the same process when they are converted into numbers.How should I do this?
Part of the table data

'longitude' 'user id'
121.470259000000 'edbc54bddf16494a49f39ac057b4185d'
121.470259000000 'f8206ab58b9bdb070673f7050242e9ee'
121.470259000000 'f8206ab58b9bdb070673f7050242e9ee'
In fact, the column related to
'user id'
should be converted to a number, and for example, rows 2 and 3, which are duplicates, should be converted in this way:
1
2
2

답변(1개)

Karim
Karim 2022년 8월 2일
An easy method would be to use the third output of the unique command. This will work directly on strings and will give the coressponding index as a number.
% create the table...
Longitude = [121.470259; 121.470259; 121.470259];
UserID = ["edbc54bddf16494a49f39ac057b4185d";"f8206ab58b9bdb070673f7050242e9ee";"f8206ab58b9bdb070673f7050242e9ee"];
MyTable = table(Longitude,UserID)
MyTable = 3×2 table
Longitude UserID _________ __________________________________ 121.47 "edbc54bddf16494a49f39ac057b4185d" 121.47 "f8206ab58b9bdb070673f7050242e9ee" 121.47 "f8206ab58b9bdb070673f7050242e9ee"
% extract the UserID and convert it into an index...
[~,~,MyTable.UserID_Idx] = unique(MyTable.UserID)
MyTable = 3×3 table
Longitude UserID UserID_Idx _________ __________________________________ __________ 121.47 "edbc54bddf16494a49f39ac057b4185d" 1 121.47 "f8206ab58b9bdb070673f7050242e9ee" 2 121.47 "f8206ab58b9bdb070673f7050242e9ee" 2
  댓글 수: 1
Tara
Tara 2022년 8월 2일
Thank you very much for your help

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by