Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

check if whole matrix zero

Asked by Light

Light (view profile)

on 7 Jun 2013

Now i combined answers. Help me about this iteration. I want it iterate until whole matrix zero.

A=[-1,1,1;0,-1,0;0,0,-1;1,0,0]
if ~sum(A(:))   % this iteration will be continued until whole matrix zero 0
   f = find(any(A==-1,2)); % i have to find row which including only one -1, if found 2 or more, i have to pick just one of them in that iteration. How can i find it?
% in one column only one -1 and 1. then after find row with only one -1, i have to add it to the row with 1 which is staying with one column. It is the way my matrix will be zero. 

2nd row which including only one -1 is added to the first row. after that:

A=[-1,0,1;0,0,0;0,0,-1;1,0,0]

If all whole matrix not zero. then will be iterated again. Please give me some clue. Help me!

Thank you for ADVICE!

2 Comments

Light

Light (view profile)

on 7 Jun 2013

My savers it can be answered one by one.

How can i use if function to iterate it. Give me some structure please

Light

Light (view profile)

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson

Walter Roberson (view profile)

on 7 Jun 2013
while true
  if (the ending condition is met)
    break
  end
  rownum = index of one row that has exactly one -1
  neg1idx = column index of -1 in row number rownum
  plus1rownum = row index of row that has +1 in column number negidx
  A(plus1rownum,:) = A(plus1rownum,:) + A(rownum, :);
end

It is, however, highly recommended that you add protections in case the input matrix is not structured the way you expect.

4 Comments

Image Analyst

Image Analyst (view profile)

on 7 Jun 2013

A matrix could sum to zero without every element being zero. If you want every element being zero, you need to take abs() before summing, or use nnz().

Walter Roberson

Walter Roberson (view profile)

on 7 Jun 2013

As I wrote in http://www.mathworks.co.uk/matlabcentral/answers/78245-check-the-matrix-result#answer_87951

~any(M(:))
will be true if M is entirely 0

Using nnz is a good idea:

nnz(M) == 0

will be true if M has only 0's in it.

Hugo

Hugo (view profile)

on 7 Jun 2013

Using the terminology of Walter Roberson but simpler

while ~(ending condition is met)
% Include the iteration code here
end

As mentioned in the previous comments, be careful with the ending condition

1) If you write

while sum(A(:) 

the code will iterate until all elements of A add up to zero. This can occur, however, even if not all elements of A are zero.

2) If you write either

while sum(abs(A(:))) 

or

while any(A(:))

then the code will iterate until all elements of A are zero (not just their sum).

NOTICE that in your comment to Walter Roberson answer, the condition if sum(A(:)) lets the iteration continue unless the sum of the elements of A is different from zero, which I think is not exactly what you want, or is it?

Best regards

Walter Roberson

Walter Roberson (view profile)

Contact us