# Logical Operators in MATLAB

1. Jul 23, 2008

### hoffmann

I solved my last problem, however, I have another question with regards to logical operators in MATLAB.

Suppose I have four column arrays "one," "two," "three," and "four." Each array contains 500 scalar values. How can I say:

If any one of these scalar values are equal to zero then execute the following code.

Obviously, I can't just say:

if(one = 0 || two = 0 || three = 0 || four = 0)

random code

end

The || operator needs a scalar input so the above wouldn't work. How can I fix this?

2. Jul 23, 2008

### hoffmann

any ideas? this is killing me...

3. Jul 24, 2008

### shoehorn

Here's a hint. You can use the 'all' command to test whether the elements of a given vector are nonzero. For instance, suppose that I have a column vector, A, defined by

Code (Text):
>> A = [1; 2; 3; 4; 5; 6];
I can test whether all the elements of A are non-zero by running

Code (Text):
>> all(A)

ans =

1
As you can see, 'all' returns 1 -- a true value -- since all of the elements of A are non-zero. On the other hand, a vector B which contains zeros would give:

Code (Text):
>> B = [1; 2; 3; 4; 0];
>> all(B)

ans =
0
since at least one of the elements of B is zero.

Think about this for a bit: it should show you how you can test whether your matrices contain zeros in an efficient manner for use in an if structure.

4. Jul 24, 2008

### hoffmann

Right, now I also need to know the locations where ANY (logical 'or' case) of the vectors that are zero. Here is what I am doing using the 'all' command:

%For the 'or' case:
rownum = find(~all([one two]'))

Now, what do I put in my if statement that takes advantage of the newly created variable 'rownum'?

5. Jul 24, 2008

### shoehorn

Your if() statement will be something of the form

Code (Text):

if((~all(A) == 1) || (~all(B) == 1) || (~all(C) == 1) || (~all(D) == 1))
% zeros are found
testMessage = ('Zeros are present!')

% Now that we know at least one zero is present, we locate precisely
% where the zeros are. Note that we I'm using if() statements here
% solely for clarity; we could make it more efficient with a bit of extra
% effort.
if(~all(A) == 1)
rownumsA = find(A == 0)
end
if(~all(B) == 1)
rownumsB = find(B == 0)
end
if(~all(C) == 1)
rownumsC = find(C == 0)
end
if(~all(D) == 1)
rownumsD = find(D == 0)
end
testMessage = ('Zeros not present!')
end
This is, of course, just a basic outline. You'll want to add more code to do something less trivial!

6. Jul 24, 2008

### hoffmann

Well here's the deal:

My code is designed such that after locating precisely where the zeros are, I have a block of code that increments a specific entry in a matrix. Is there any way I could combine all the code in the 'if' statements and, underneath, increment my matrix? I could post code if necessary.

Thanks!

7. Jul 24, 2008

### hoffmann

Here's the matrix I want to create and increment:

in1=in1(1:len);
in2=in2(1:len);
out1=out1(1:len);
out2=out2(1:len);

inter=zeros(length(list), length(list)); %initialize a matrix

for i = 1:length(in1)

inter(in1(i), in2(i)) = inter(in1(i), in2(i))+1;
inter(in2(i), in1(i)) = inter(in2(i), in1(i))+1;

inter(in1(i), out1(i)) = inter(in1(i), out1(i))+1;
inter(out1(i), in1(i)) = inter(out1(i), in1(i))+1;

inter(in1(i), out2(i)) = inter, out2(i))+1;
inter(out2(i), in1(i)) = inter), in1(i))+1;

inter(in2(i), out1(i)) = inter, out1(i))+1;
inter(out1(i), in2(i)) = inter(out1(i), in2(i))+1;

inter(in2(i), out2(i)) = inter(in2(i), out2(i))+1;
inter(out2(i), in2(i)) = inter(out2(i), in2(i))+1;

inter(out1(i), out2(i)) = inter(out1(i), out2(i))+1;
inter(out2(i), out1(i)) = inter(out2(i), out1(i))+1;

end

in1, in2, out1, and out2 are the four vectors holding my data. all four vectors holding my data are the same size. inter is the matrix that I want to create. list is the input txt file which contains my data.

Last edited: Jul 24, 2008
8. Jul 24, 2008

### hoffmann

Here is my revised version with what I think is functional. Please let me know if there is anything wrong with the code:

in1=in1(1:len);
in2=in2(1:len);
out1=out1(1:len);
out2=out2(1:len);

%rowNum=find( ~all([in1 in2 out1 out2]) ); % locations where ANY ("or") of the species are zero

rowNum_in1=find(in1 == 0);
rowNum_in2=find(in2 == 0);
rowNum_out1=find(out1 == 0);
rowNum_out2=find(out2 == 0);

%if ((~all(in1) == 1 || ~all(in2) == 1 || ~all(out1) == 1 || ~all(out2) == 1))

% disp(['Zeros are present.'])

%end

inter=zeros(length(list), length(list)); %initialize a matrix

for i = 1:length(in1)

if ( ~all(rowNum_in1) == 1 || ~all(rowNum_in2) == 1 || ~all(rowNum_out1) == 1 || ~all(rowNum_out2) == 1)

inter(in1(i), in2(i)) = inter(in1(i), in2(i))+1;
inter(in2(i), in1(i)) = inter(in2(i), in1(i))+1;

inter(in1(i), out1(i)) = inter(in1(i), out1(i))+1;
inter(out1(i), in1(i)) = inter(out1(i), in1(i))+1;

inter(in1(i), out2(i)) = inter, out2(i))+1;
inter(out2(i), in1(i)) = inter), in1(i))+1;

inter(in2(i), out1(i)) = inter, out1(i))+1;
inter(out1(i), in2(i)) = inter(out1(i), in2(i))+1;

inter(in2(i), out2(i)) = inter(in2(i), out2(i))+1;
inter(out2(i), in2(i)) = inter(out2(i), in2(i))+1;

inter(out1(i), out2(i)) = inter(out1(i), out2(i))+1;
inter(out2(i), out1(i)) = inter(out2(i), out1(i))+1;

end
end