-
Notifications
You must be signed in to change notification settings - Fork 1
/
AinB.m
38 lines (34 loc) · 990 Bytes
/
AinB.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function bool = AinB(A,B)
% AINB Determine if string A is an element in cell array B. Please just
% call any(strcmp(A,B)) instead of using this function.
%
% Inputs:
% A Character vector.
% B Cell array.
%
% Outputs:
% bool Whether A is in B (1), or not(0).
%
% Examples:
% AinB('a',{'a','b','c'}) -> true
% AinB('a',{'abc'}) -> false
% AinB({'a'},{{'a'}}) -> bad input results not guaranteed
% AinB('a',{{'a'}}) -> false
bool = any(strcmp(A,B));
end
function bool = AinB_1(A,B)
% Another method to do this using built-in functions:
% ~isempty(find(strcmp(A,B)))
bool = ~isempty(find(strcmp(A,B)));
end
function bool = AinB_2(A,B)
bool = false;
if ischar(A) && iscell(B)
for i = 1:length(B)
if ischar(B{i}) && strcmp(A,B{i})
bool = true;
return
end
end
end
end