Logical indexing in cell array
125 次查看(过去 30 天)
显示 更早的评论
Is there a way to search strings in a cell array similar to numeric arrays?
a = [1 2 3 4 5 6];
>> idx = find(a==3)
idx = 3
>> b = {'1' '2' '3' '4' '5' '6'};
>> idx = find(b=='3')
Undefined function 'eq' for input arguments of type 'cell'.
0 个评论
采纳的回答
Image Analyst
2016-10-14
Use ismember to search cell arrays:
b = {'1' '2' '3' '4' '5' '6'};
logicalIndex = ismember(b, '3') % Or...
actualIndex = find(ismember(b, '3'))
0 个评论
更多回答(3 个)
michio
2016-10-14
Using cellfun is one way.
b = {'1' '2' '3' '4' '5' '6'};
cellfun(@(x) strcmp(x,'3'), b)
Sulaymon Eshkabilov
2021-7-4
Now, what michio suggested works perfectly ok:
b = {'1' '2' '3' '4' '5' '6'};
b(cellfun(@(x) strcmp(x,'3'), b))={'Found 3'}
So this is another good solution for this exercise.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!