Find last non-zero in a given dimension - MATLAB Cody - MATLAB Central

Problem 340. Find last non-zero in a given dimension

Difficulty:Rate

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

26.44% Correct | 73.56% Incorrect
Last Solution submitted on Jan 17, 2025

Problem Comments

Solution Comments

Show comments
PIVlab surpasses 100K all-time File Exchange downloads
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle...
4
8
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
4

Problem Recent Solvers39

Suggested Problems

Problem Tags

Community Treasure Hunt

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

Start Hunting!