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'
?
如果我想找到“KU”的索引,该怎么办?
8 个解决方案
#1
114
I guess the following code could do the trick:
我想下面的代码可以做到这一点:
strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ind=find(ismember(strs,'KU'))
This returns
这将返回
ans =
2
#2
84
>> strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
>> tic; ind=find(ismember(strs,'KU')); toc
Elapsed time is 0.001976 seconds.
运行时间是0.001976秒。
>> tic; find(strcmp('KU', strs)); toc
Elapsed time is 0.000014 seconds.
运行时间是0.000014秒。
SO, clearly strcmp('KU', strs)
takes much lesser time than ismember(strs,'KU')
因此,显然strcmp('KU', strs)的时间比ismember(strs,'KU')要小得多
#3
39
Since 2011a, the recommended way is:
自2011a以来,推荐的方式为:
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.
strfind被弃用,所以尽量不要使用它。
#4
22
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 :).
你不会担心的。
#5
12
Other answers are probably simpler for this case, but for completeness I thought I would add the use of cellfun with an anonymous function
对于这种情况,其他的答案可能比较简单,但是为了完整性起见,我认为我应该添加一个匿名函数来使用cellfun。
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))
#6
5
The strcmp and strcmpi functions are the most direct way to do this. They search through arrays.
strcmp和strcmpi函数是最直接的方法。他们搜索数组。
strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ix = find(strcmp(strs, 'KU'))
#7
5
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
.
但它只返回strs中的第一个位置。如果未找到元素,则ind=0。
#8
-2
did you try
你试过
indices = Find(strs, 'KU')
see link
看到链接
alternatively,
另外,
indices = strfind(strs, 'KU');
should also work if I'm not mistaken.
如果我没有弄错的话,也应该工作。
#1
114
I guess the following code could do the trick:
我想下面的代码可以做到这一点:
strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ind=find(ismember(strs,'KU'))
This returns
这将返回
ans =
2
#2
84
>> strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
>> tic; ind=find(ismember(strs,'KU')); toc
Elapsed time is 0.001976 seconds.
运行时间是0.001976秒。
>> tic; find(strcmp('KU', strs)); toc
Elapsed time is 0.000014 seconds.
运行时间是0.000014秒。
SO, clearly strcmp('KU', strs)
takes much lesser time than ismember(strs,'KU')
因此,显然strcmp('KU', strs)的时间比ismember(strs,'KU')要小得多
#3
39
Since 2011a, the recommended way is:
自2011a以来,推荐的方式为:
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.
strfind被弃用,所以尽量不要使用它。
#4
22
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 :).
你不会担心的。
#5
12
Other answers are probably simpler for this case, but for completeness I thought I would add the use of cellfun with an anonymous function
对于这种情况,其他的答案可能比较简单,但是为了完整性起见,我认为我应该添加一个匿名函数来使用cellfun。
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))
#6
5
The strcmp and strcmpi functions are the most direct way to do this. They search through arrays.
strcmp和strcmpi函数是最直接的方法。他们搜索数组。
strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ix = find(strcmp(strs, 'KU'))
#7
5
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
.
但它只返回strs中的第一个位置。如果未找到元素,则ind=0。
#8
-2
did you try
你试过
indices = Find(strs, 'KU')
see link
看到链接
alternatively,
另外,
indices = strfind(strs, 'KU');
should also work if I'm not mistaken.
如果我没有弄错的话,也应该工作。