将单元格中的字符串替换为整数 ID - Matlab

发布于 2024-11-15 02:16:44 字数 576 浏览 2 评论 0原文

我有一个带有字符串 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-11-22 02:16:44

您可以使用 ismember 函数和逻辑索引来实现你想要的。

[~,indx]=ismember(celldata(:,1),intIDs(:,1));
celldata(:,1)=intIDs(indx,2)

celldata = 

    [1]    [0.1000]
    [2]    [0.6430]
    [3]    [0.4350]
    [4]    [0.3400]
    [1]    [0.1200]
    [2]    [1.5000]
    [3]    [0.7500]
    [1]    [0.5600]
    [2]    [0.6800]
    [3]    [0.9700]

You can use the ismember function and logical indexing to achieve what you want.

[~,indx]=ismember(celldata(:,1),intIDs(:,1));
celldata(:,1)=intIDs(indx,2)

celldata = 

    [1]    [0.1000]
    [2]    [0.6430]
    [3]    [0.4350]
    [4]    [0.3400]
    [1]    [0.1200]
    [2]    [1.5000]
    [3]    [0.7500]
    [1]    [0.5600]
    [2]    [0.6800]
    [3]    [0.9700]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文