find rows of Zeros in matrix
조회 수: 12 (최근 30일)
이전 댓글 표시
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.
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!