What other command can I use instead of isempty?
이전 댓글 표시
If I have A = '1' and B = {'2', '3', '4'} And naturally my strfind(B, A) returns ans =
[] [] []
I try to use this in an if statement with
if ~isempty(strfind(B, A))
condition 1
else
condition 2
end
and hope that this will execute condition 2, however it turns out isempty only returns logical 1 when used with strfind... How can I modify my code? And any suggestions on which command works faster between using strfind() and find()? Thank you as always!
채택된 답변
추가 답변 (2개)
You don't have to "guess" how MATLAB works: the helpful workers at TMW spent many thousands of hours writing documentation that tell all of us how MATLAB works, and it is trivial to find (and read) using an internet browser.
As the documentation clearly states (did you read it?), when the first strfind input str is a cell array, then the output is also a cell array: "If str is a cell array of character vectors, strfind returns a cell array of vectors of type double. " Further down it even explains the size that this cell array will have.
Lets have a look at it:
>> A = '1';
>> B = {'2', '3', '4'};
>> X = strfind(B, A)
X =
[] [] []
>> class(X)
ans =
cell
>> size(X)
ans =
1 3
Is the output empty? No, it has size 1x3, the same size as the input array B, just as the documentation says it will have. X certainly will not be empty if B is not empty. If you want to know if the contents of X are empty, then you will need to use some other method, e.g.:
>> ~cellfun('isempty',X) % you might want |any| or |all|
ans =
0 0 0
or even simply
>> ismember(B,A)
ans =
0 0 0
댓글 수: 2
chlor thanks
2016년 8월 5일
편집: chlor thanks
2016년 8월 5일
chlor thanks
2016년 8월 5일
편집: chlor thanks
2016년 8월 5일
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!