to use ismember with arraycell
20 ビュー (過去 30 日間)
古いコメントを表示
gg =
4×1 cell array
{'A' }
{'A'}
{'B' }
{'A' }
[a,b]=ismember({'A'},gg)
a =
logical
1
b =
1
is not corret..i expect 1 1 0 1
1 件のコメント
Stephen23
2024 年 10 月 13 日
"is not corret.."
It is correct: the first input 'A' is definitely a member of the second input:
ismember('A',{'A','A','B','A'})
"i expect 1 1 0 1"
ismember({'A','A','B','A'},'A')
採用された回答
Sameer
2024 年 10 月 13 日
編集済み: Sameer
2024 年 10 月 13 日
Hi Luca
The "ismember" function is designed to work with arrays, and when using it with cell arrays of strings, it checks for membership of each element in the cell array. However, it returns a single logical value for the entire array when you provide a single element to check against the cell array.
To achieve the result you expect , you can use "strcmp" to compare each element in the cell array individually.
Here's how you can do it:
gg = {'A'; 'A'; 'B'; 'A'};
a = strcmp('A', gg);
disp(a);
Please refer to the below MathWorks documentation link:
Hope this helps!
1 件のコメント
Walter Roberson
2024 年 10 月 13 日
[a,b]=ismember({'A'},gg)
answers the question of whether 'A' occurs in gg, as a yes or no answer in a and the index inside gg it occurs in b . {'A'} is a single element so you get a single answer.
[a,b]=ismember(gg, {'A'})
on the other hand iterates through the elements of gg, and for each one answers the question of whether the element occurs within {'A'} . gg is length 4 so the answer in a will be of length 4 -- [gg{1} is in {'A'}, gg{2} is in {'A'}, gg{3} is in {'A'}, gg{4} is in {'A'}] .
その他の回答 (2 件)
Pavl M.
2024 年 10 月 13 日
gg = {'A' ,'A','B' ,'A' }
a = cell2mat(gg)
g = zeros(1,length(a));
g(find(a=='A')) = 1
7 件のコメント
Pavl M.
2024 年 10 月 14 日
編集済み: Pavl M.
2024 年 10 月 14 日
clc
clear all
close all
%% Inputs ( from a methode ):
ex = 'STA';
dd = { 'STA', 'STAT', 'STA', 'STATT' } ;
%% Detection module:
% contains(dd, 'STA') is not yet implemented as built-in in TCE Octave
if length(ex)>1
strcmp(dd,ex)
else
a = cell2mat(dd);
g = zeros(1,length(a));
g(find(a==ex)) = 1
end
Steven Lord
2024 年 10 月 13 日
Both outputs are the same size as the first input.
gg = {'A'; 'A'; 'B'; 'A'};
[a1,b1]=ismember({'A'},gg)
[a2, b2] = ismember(gg, {'A'})
2 件のコメント
Steven Lord
2024 年 10 月 14 日
Based on a revised example you posted as a comment on another answer, it looks like you're not trying to find cells in a cell array that are an exact match for a specific character. It looks like you're trying to find cells that contain that character anywhere inside the word they contain. If that is correct:
gg = { 'STA' , 'STAT' , 'STA' , 'STATT' }
contains(gg, 'A')
gg{3} = 'STEM' % Replace one of the words with one not containing 'A'
contains(gg, 'A')
Or you could use other string related functions instead of contains. Some examples include startsWith and endsWith.
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!