gpt4 book ai didi

algorithm - 数据清洗情绪分析

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:52:04 26 4
gpt4 key购买 nike

假设我们有一个向量(输入):

v = ["apple", "man", "ship", " man", " apple"]

所需的输出应该类似于:

y = [[1,5],[2,4]]

本质上,y 是一个数组数组,其中包含由“空格”字符(例如“apple”和“apple”)不同的单词的索引。

关于如何在 MATLAB 中完成此操作有什么建议吗?

最佳答案

我假设您希望 y 是一个二维数组,其中每一行都包含一个字符串的索引,后跟它的空格填充版本。您可以使用函数 strip 来做到这一点(或 strtrim ),setdiff , 和 ismember像这样:

[strWithSpace, index1] = setdiff(v, strip(v));
[~, index2] = ismember(strip(strWithSpace), v);
y = [index2(:) index1(:)];

以及您的示例案例的结果:

y =

1 5
2 4

请注意,以上内容同样适用于 string array。因为它将用于 cell array of character arrays .因此,您的输入可能是以下之一:

v = ["apple", "man", "ship", " man", " apple"];
v = {'apple', 'man', 'ship', ' man', ' apple'};

关于algorithm - 数据清洗情绪分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47641218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com