MATLAB How to Handle Zero Values in MATLAB Arrays?

  • Thread starter Thread starter hoffmann
  • Start date Start date
  • Tags Tags
    Matlab Operators
AI Thread Summary
To handle zero values in MATLAB arrays, the discussion emphasizes using logical operators effectively. The 'all' command can be utilized to check if all elements in a vector are non-zero, allowing for efficient conditional statements. The proposed solution involves creating a variable to identify the locations of zeros and incorporating this into an if statement to execute specific code when zeros are found. Additionally, the conversation explores optimizing the code for incrementing a matrix based on the identified zero locations. The final code revisions aim to streamline the process while ensuring functionality.
hoffmann
Messages
65
Reaction score
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
any ideas? this is killing me...
 
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.
 
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'?
 
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!
 
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!
 
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:
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
 

Similar threads

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