How to Handle Zero Values in MATLAB Arrays?

  • Context: MATLAB 
  • Thread starter Thread starter hoffmann
  • Start date Start date
  • Tags Tags
    Matlab Operators
Click For Summary

Discussion Overview

The discussion revolves around handling zero values in MATLAB arrays, specifically focusing on logical operators and conditional statements. Participants explore methods to check for zero values across multiple column arrays and how to efficiently locate these zeros for further processing in their code.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes a scenario with four column arrays and seeks a method to check if any of the values are zero, noting that the logical 'or' operator requires scalar inputs.
  • Another participant suggests using the 'all' command to determine if all elements in a vector are non-zero, providing examples to illustrate this approach.
  • A participant inquires about how to find the locations of zeros in the arrays, referencing their use of the 'all' command and asking for guidance on incorporating this into an if statement.
  • Further discussion includes a request for advice on combining multiple if statements to increment a specific entry in a matrix based on the presence of zeros.
  • One participant shares a code snippet for initializing and incrementing a matrix based on the indices of zero values found in the input vectors.
  • A later post presents a revised version of the code and asks for feedback on its functionality, highlighting the use of find operations to locate zeros in each vector.

Areas of Agreement / Disagreement

Participants express various approaches to the problem, but there is no consensus on a single solution or method. Multiple strategies are discussed, and some participants seek clarification or further assistance without resolving the overall question.

Contextual Notes

Some code snippets contain potential syntax errors or unclear logic, which may affect their functionality. Participants are encouraged to refine their code further, but specific corrections are not provided in the discussion.

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 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
2
Views
3K