How to determine if there is a value in a string and output it as a boolean?

i.e,
A = [0 1 2 3 0 0 0]
I want to write:
-----------------
if % A(a)= 1%
content
end
---------
where 1<a<end.
so if at any index in A, there exists a A='1', the if statement is true and it is executed
Thanks

1 件のコメント

Jan
Jan 2014 年 2 月 23 日
@Amar: Please do not post a question twice. Double posting confuses the ones, who want to help you.

サインインしてコメントする。

回答 (2 件)

Mischa Kim
Mischa Kim 2014 年 2 月 23 日
編集済み: Mischa Kim 2014 年 2 月 23 日
Amar, you could use
a = 3;
if find(A==a)
...
end
Note, that in your case A is not a string but numeric array. To compare (find) characters or sub-strings in a string you'd use strcmp.

3 件のコメント

Amar
Amar 2014 年 2 月 23 日
I'm sorry I might have expressed myself incorrectly. Please read the question again for I have made further elaboration
Mischa Kim
Mischa Kim 2014 年 2 月 23 日
編集済み: Mischa Kim 2014 年 2 月 23 日
Something like:
A = {'0' '1' '2' '3' '0' '0'}; % cell array of char
a = '1';
if sum(strcmp(a,A))>0
fprintf('Character ''%s'' found in A\n',a);
else
fprintf('Character ''%s'' not found in A\n',a);
end
Jan
Jan 2014 年 2 月 23 日
編集済み: Jan 2014 年 2 月 23 日
find(A==a) is tricky, because it can reply arrays without an element or withmultiple elements also. Then this is performed internally:
if all(find(A==a)) && ~isempty(find(A==a))
This implicit behaviour might be confusing, so I'd suggest to write explicitly:
if any(A==a)
While sum(strcmp(a,A))>0 is clear, the summation is less efficient than any().

サインインしてコメントする。

David Young
David Young 2014 年 2 月 23 日
I think you just want
if A(a) == 1
% actions
end

1 件のコメント

Amar
Amar 2014 年 3 月 2 日
I have no idea why I went full retarded lol. I am not a smart man.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFunctions についてさらに検索

タグ

質問済み:

2014 年 2 月 23 日

コメント済み:

2014 年 3 月 2 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by