如何在 MATLAB 元胞数组中搜索字符串?

发布于 2024-12-14 05:11:03 字数 121 浏览 0 评论 0原文

假设我有元胞数组,

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}

如果我想查找 'KU' 的索引,该怎么办?

Let's say I have the cell array

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}

What should I do if I want to find the index of 'KU'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

陈甜 2024-12-21 05:11:04

strcmp 和 strcmpi 函数是执行此操作的最直接方法。他们通过数组进行搜索。

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ix = find(strcmp(strs, 'KU'))

The strcmp and strcmpi functions are the most direct way to do this. They search through arrays.

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ix = find(strcmp(strs, 'KU'))
拿命拼未来 2024-12-21 05:11:04

您是否尝试

indices = Find(strs, 'KU')

查看 链接

或者,

indices = strfind(strs, 'KU');

如果我不这样做也应该可以错误。

did you try

indices = Find(strs, 'KU')

see link

alternatively,

indices = strfind(strs, 'KU');

should also work if I'm not mistaken.

风轻花落早 2024-12-21 05:11:03

我想下面的代码可以做到这一点:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ind=find(ismember(strs,'KU'))

这返回

ans = 
     2

I guess the following code could do the trick:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ind=find(ismember(strs,'KU'))

This returns

ans = 
     2
腹黑女流氓 2024-12-21 05:11:03
>> strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
>> tic; ind=find(ismember(strs,'KU')); toc

已用时间为 0.001976 秒。

>> tic; find(strcmp('KU', strs)); toc

已用时间为 0.000014 秒。

所以,显然 strcmp('KU', strs)ismember(strs,'KU') 花费的时间少得多

>> strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
>> tic; ind=find(ismember(strs,'KU')); toc

Elapsed time is 0.001976 seconds.

>> tic; find(strcmp('KU', strs)); toc

Elapsed time is 0.000014 seconds.

SO, clearly strcmp('KU', strs) takes much lesser time than ismember(strs,'KU')

月亮邮递员 2024-12-21 05:11:03

从2011a开始,推荐的方式是:

booleanIndex = strcmp('KU', strs)

如果你想获取整数索引(你通常不需要),你可以使用:

integerIndex = find(booleanIndex);

strfind已被弃用,所以尽量不要使用它。

Since 2011a, the recommended way is:

booleanIndex = strcmp('KU', strs)

If you want to get the integer index (which you often don't need), you can use:

integerIndex = find(booleanIndex);

strfind is deprecated, so try not to use it.

猫七 2024-12-21 05:11:03

我发现每个人都错过了代码中最重要的缺陷:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}

应该是:

strs = {'HA' 'KU' 'NA' 'MA' 'TATA'} 

或者

strs = {'HAKUNA' 'MATATA'}

现在如果您坚持使用

ind=find(ismember(strs,'KU'))

您将拥有 无忧无虑:)。

I see that everybody missed the most important flaw in your code:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}

should be:

strs = {'HA' 'KU' 'NA' 'MA' 'TATA'} 

or

strs = {'HAKUNA' 'MATATA'}

Now if you stick to using

ind=find(ismember(strs,'KU'))

You'll have no worries :).

や莫失莫忘 2024-12-21 05:11:03

对于这种情况,其他答案可能更简单,但为了完整性,我想我会添加使用 cellfun 和匿名函数

indices = find(cellfun(@(x) strcmp(x,'KU'), strs))

,其优点是您可以轻松地使其不区分大小写,或者在具有结构单元数组的情况下使用它:

indices = find(cellfun(@(x) strcmpi(x.stringfield,'KU'), strs))

Other answers are probably simpler for this case, but for completeness I thought I would add the use of cellfun with an anonymous function

indices = find(cellfun(@(x) strcmp(x,'KU'), strs))

which has the advantage that you can easily make it case insensitive or use it in cases where you have cell array of structures:

indices = find(cellfun(@(x) strcmpi(x.stringfield,'KU'), strs))
木落 2024-12-21 05:11:03

最短的代码:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
[~,ind]=ismember('KU', strs)

但它仅返回 strs 中的第一个位置。如果未找到元素,则 ind=0

Most shortest code:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
[~,ind]=ismember('KU', strs)

But it returns only first position in strs. If element not found then ind=0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文