0

I am looking to achieve something that seems fairly simple, just not sure how to implement ORs in an IF statement:

A = [4 6 7; 3 4 7; 8 7 1]
C = 6
if C is in first row of A 
(i.e. if row 1 contains 6, basically -- IF C = 4 or C=6 or C=7)    
    (then do this)
end

Any suggestions?

4

2 回答 2

2

所以

A(1,:) == C

是一个开始。在您的情况下,它将返回一个 3 元素布尔数组,其中

ans(1) = 1 if A(1,1) == C, 0 otherwise
ans(2) = 1 if A(1,2) == C, 0 otherwise
ans(3) = 1 if A(1,3) == C, 0 otherwise

从那里,你可以做类似的事情

if( sum(A(1,:) == C) )
# or
if( length(find(A(1,:) == C)) )

会工作。

于 2012-10-13T01:21:21.547 回答
0
   A = [4 6 7; 3 4 7; 8 7 1];
   C = 6;

   rowNum = 1;

   if (sum(A(rowNum,:) == C) ~= 0)
      do something
   end
于 2012-10-13T14:23:04.913 回答