What other command can I use instead of isempty?

39 ビュー (過去 30 日間)
chlor thanks
chlor thanks 2016 年 8 月 5 日
編集済み: chlor thanks 2016 年 8 月 5 日
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!

採用された回答

Guillaume
Guillaume 2016 年 8 月 5 日
if ~isempty(strfind(B, A))
condition 1
else
condition 2
end
The problem is that, even conceptually, this does not express what you actually want, so it's no wonder it does not work.
You're not asking if A is found in B (in which case, using isempty(strfind(...)) would work). You want to ask either (it's not clear from your question) if A is found in all of the Bs, or in any of the Bs. That's a completely different question, hence the syntax is different.
strfind when given a cell array tells you, for each element of B, this is where I found A. Therefore, you have to ask strfind: for each element of B is the return value empty. And finally, you have to apply the any or all operator (depending on what you want) to that answer. So:
if any(~cellfun(@isempty, strfind(B, A))) %possibly replace any by all
condition1
else
condition2
end
  1 件のコメント
chlor thanks
chlor thanks 2016 年 8 月 5 日
編集済み: chlor thanks 2016 年 8 月 5 日
omg I didn't see your post until I updated my comment on Stephen's post after I search through more answers and figure out there is actually a command called "any"... This saved me so much less code as I was gonna do a for loop... Thanks Guillaume you always have the best answer!

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

その他の回答 (2 件)

Stephen23
Stephen23 2016 年 8 月 5 日
編集済み: Stephen23 2016 年 8 月 5 日
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
chlor thanks 2016 年 8 月 5 日
編集済み: chlor thanks 2016 年 8 月 5 日
I did look them up and figured out why this method won't work, however as a beginner I only know a few command off hand and wonder is there other command I can use in this case, thanks for taking your time explaining the concepts more explicitly! I updated the title... I apologize for the misleading title that I posted earlier.
chlor thanks
chlor thanks 2016 年 8 月 5 日
編集済み: chlor thanks 2016 年 8 月 5 日
Silly me, I just discovered using any()
if any(~cellfun('isempty', (strfind(B, A)))
condition 1
else
condition 2
end
With cellfun this works great! This makes much more sense to me now. Thank you Stephen!

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


Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 5 日
Use
ismember(B,A)
  1 件のコメント
chlor thanks
chlor thanks 2016 年 8 月 5 日
I try to avoid using ismember as I wish to find a string as part of the cell array instead the exact string inside that cell array...

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by