Creating a function that removes full rows of non zero values
4 views (last 30 days)
Show older comments
I am struggling to create a function that looks at each row of a matrix to see if it contains all non zero numbers. If it does, those rows get removed. Then, the remaining rows are shifted down and the number of rows that are removed need to get added back on the top as rows of zeros. So if two full rows are removed, everything should get shifted down and then two rows of zeros should be added to the top.
1 Comment
Steven Lord
on 2 Apr 2025
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Answers (1)
Walter Roberson
on 2 Apr 2025
The key here is to check
mask = any(MATRIX == 0, 2)
If there is at least one zero, then not all of the entries in the row are non-zero. So the rows selected by mask are the rows to keep.
For the other part: consider the sum(mask) ...
0 Comments
See Also
Categories
Find more on Logical 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!