find rows of Zeros in matrix

조회 수: 12 (최근 30일)
Jennifer Samson
Jennifer Samson 2018년 10월 8일
댓글: Jennifer Samson 2018년 10월 8일
Hi! I am wondering how to determine, in code, that there is a full row of zeros in a matrix? I am doing a Gauss Elimination and I need to return an error code if a row of zeros appears in my matrix after the first step.
  댓글 수: 2
madhan ravi
madhan ravi 2018년 10월 8일
an example?
Jennifer Samson
Jennifer Samson 2018년 10월 8일
well in Gauss Elimination the first step I have going is to convert my first matrix into an upper diagonal matrix, and the next step is to back calculate and solve for my x vector, but it there is a full row of zeros there are no unique solutions
example
[1 2 3] [4 5 6] [7 8 9]
[1;2;3]
transforms to [1 2 3 1] [4 5 6 2] [7 8 9 3]
transforms to
[1 2 3 1] [0 -3 -6 -2] [0 0 0 0]
and this matrix results in a non real answer when it is back calculated, I need a code that identifies the row of zeros and then returns an error message instead of continuing to the nest step.

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2018년 10월 8일
Time to learn MATLAB. How do you find that something is zero?
A == 0
That returns 1 where an element of A is zero. EXACTLY zero.
Next, you want to know if an entire row of the matrix is zero. What tool will tell you that? What does the all function do? So how about this?
all(A == 0,2)
That returns true for any row that is entirely zero.
How do you find which row is zero? I suppose you could use find.
So, will that work? Almost surely, certainly, positively, NOT. Why not? Because the elements you are testing will not be EXACTLY zero. So you need to learn why that happens. For example, surely this is zero:
.3 -.2 -.1
ans =
-2.77555756156289e-17
Or, not. I hope you have had some discussions about floating point arithmetic by now.
The fix is usually to test if the absolute value of the elements are less than some tolerance, NOT for exact equality with zero.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by