将单元格中的字符串替换为整数 ID - Matlab
我有一个带有字符串 ID 的单元格。我需要将它们替换为整数 ID,以便单元格可以转换为矩阵。我特别需要这是一个矢量化操作,因为单元数据很大。
celldata = { 'AAPL' [0.1] ; '谷歌'[0.643]; “IBM”[0.435]; '嗯' [0.34] ; “AAPL”[0.12]; '谷歌'[1.5]; “IBM”[0.75]; “AAPL”[0.56]; '谷歌'[0.68]; “IBM”[0.97]; };
我设计了一个顺序 intID:
intIDs = {'AAPL' [1] ; '谷歌' [2] ; “IBM”[3]; 'MMM' [4]};
intIDs 包含单元数据中可能存在的所有 ID。此外,celldata 具有按顺序排列的 ID,并按日期分组。此处未显示日期列。
期望的结果:
celldata = {[1] [0.1] ; [2][0.643]; [3] [0.435]; [4][0.34]; [1][0.12]; [2][1.5]; [3][0.75]; [1][0.56]; [2][0.68]; [3] [0.97] ;};
谢谢!
I have a cell that has string IDs. I need to replace them with integer IDs so that the cell can be transformed into a matrix. I especially need this to be a vectorized operation as the celldata is huge.
celldata = { 'AAPL' [0.1] ; 'GOOG' [0.643] ; 'IBM' [0.435] ; 'MMM' [0.34] ; 'AAPL' [0.12] ; 'GOOG' [1.5] ; 'IBM' [0.75] ; 'AAPL' [0.56] ; 'GOOG' [0.68] ; 'IBM' [0.97] ; };
I designed a sequential intID:
intIDs = {'AAPL' [1] ; 'GOOG' [2] ; 'IBM' [3] ; 'MMM' [4]};
intIDs contain ALL IDs that are possible in celldata. Also, celldata has IDs in sequential order and grouper together by dates. The date column is not shown here.
Desired result:
celldata = {[1] [0.1] ; [2] [0.643] ; [3] [0.435] ; [4] [0.34] ; [1] [0.12] ; [2] [1.5] ; [3] [0.75] ; [1] [0.56] ; [2] [0.68] ; [3] [0.97] ;};
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ismember
函数和逻辑索引来实现你想要的。You can use the
ismember
function and logical indexing to achieve what you want.