How to make an efficient if-statement-expression that when identifying a 1x3 vector consisting only of either 6's,7's,8's or 9's gives the same statement?

I could you run following if statement
vec=[9 9 9]; if vec(1:3)==9 | vec(1:3)==8 | vec(1:3)==7 | vec(1:3)==6 disp('Identified') end Identified
But is there not a more efficient way to accomplish this in stead of writing all these 'or-signs' in the if statement? With vectorized code? I have tried with a for loops, but got problems when writing it under an if statement with more than one evaluation option like elseif

 採用された回答

Matt J
Matt J 2012 年 11 月 22 日
if ismember(vec,6:9)

4 件のコメント

John
John 2012 年 11 月 22 日
The identified 1x3 vector should include the same number in each element. That means it should only allow vectors like [9 9 9] or [8 8 8] and give the same statement. I hope my question is clear now.
Matt J
Matt J 2012 年 11 月 22 日
編集済み: Matt J 2012 年 11 月 22 日
Then you should edit your question, since the code you posted there isn't equivalent to what you've now described.
The modification of my approach that you need is
if ismember(vec,(6:9).'*[1 1 1],'rows')
I have tried the same code on a case where the if-statement-expression should (again) identify a 1x3 vector but with two identical elements ranging from 1:10. I was forced to split the 1x3 vector into three 1x2 vectors as following
vec=[1 2 2];
if ismember(vec(1:2),(1:10).'*[1 1],'rows') || ismember(vec(2:3),(1:10).'*[1 1],'rows') || ismember(vec(1:2:3),(1:10).'*[1 1],'rows')
display('Identified')
end
Identified
Is there a method to accomplish this in less steps?
if isequal( sort(nonzeros(histc(vec,1:10)))' , [1 2])

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

その他の回答 (1 件)

カテゴリ

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

質問済み:

2012 年 11 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by