How to Handle Zero Values in MATLAB Arrays?

In summary: I need to increment each of these vectors. I tried adding the code in the 'if' statements to increment the matrices, but it doesn't seem to work. Can you help me out?In summary, the code in the 'if' statements is trying to increment the matrices, but it doesn't seem to be working.
  • #1
hoffmann
70
0
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 anyone 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?
 
Physics news on Phys.org
  • #2
any ideas? this is killing me...
 
  • #3
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:
>> A = [1; 2; 3; 4; 5; 6];

I can test whether all the elements of A are non-zero by running

Code:
>> 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:
>> 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
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
hoffmann said:
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'?

Your if() statement will be something of the form

Code:
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
else    % zeros are not found
    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
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
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:
  • #8
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
 

FAQ: How to Handle Zero Values in MATLAB Arrays?

1. What are logical operators in MATLAB?

In MATLAB, logical operators are symbols or keywords that are used to compare two values or conditions and return a logical value (true or false). These operators are commonly used in conditional statements to control the flow of a program.

2. What are the different types of logical operators in MATLAB?

There are three main types of logical operators in MATLAB: relational operators, logical operators, and bitwise operators. Relational operators compare two values and return a logical value, logical operators combine multiple conditions and return a logical value, and bitwise operators perform operations on binary representations of values.

3. How do I use logical operators in MATLAB?

To use logical operators in MATLAB, you need to use them within conditional statements such as if/else statements or loop statements. For example, you can use the logical operator "&&" (AND) to check if two conditions are both true before executing a certain block of code.

4. Can I use logical operators with non-numeric data in MATLAB?

Yes, in addition to using logical operators with numeric data, you can also use them with non-numeric data such as strings and characters. MATLAB will convert the non-numeric data to a numeric representation and then perform the comparison.

5. What happens if I use multiple logical operators in a single statement in MATLAB?

If you use multiple logical operators in a single statement in MATLAB, the evaluation will follow the order of operations (similar to arithmetic operations). The precedence of logical operators is as follows: parentheses, NOT, AND, OR. It is important to use parentheses to ensure the desired evaluation order.

Similar threads

Replies
32
Views
3K
Replies
2
Views
1K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
2
Views
3K
Replies
1
Views
1K
Back
Top