find rows of Zeros in matrix
12 views (last 30 days)
Show older comments
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.
Answers (1)
John D'Errico
on 8 Oct 2018
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 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!