Problem 340. Find last non-zero in a given dimension
You are given a logical matrix BW of any dimension, and a dimension dim. You need to find the locations of the last non-zero element of BW in that given dimension. If no non-zeros exist at an element location, return a 0 at that location.
For example, given:
BW = [0 0 0 0; 1 1 1 1; 0 1 1 0]
When dim = 1, you should return:
ans = [2 3 3 2], because these are the row numbers of the last non-zeroes in each column.
When dim = 2, you should return:
ans = [0; 4; 3], because these are the column numbers of the last non-zeroes in each row.
Don't forget that the input isn't restricted to 2D matrices.
Solution Stats
Problem Comments
-
1 Comment
What is the 3 and 4 dimension in matrix? How is it used?
Solution Comments
Show commentsProblem Recent Solvers38
Suggested Problems
-
Increment a number, given its digits
642 Solvers
-
Omit columns averages from a matrix
580 Solvers
-
Set the array elements whose value is 13 to 0
1351 Solvers
-
Cell Counting: How Many Draws?
1834 Solvers
-
1372 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!