MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

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.

14 correct solutions
28 incorrect solutions

Last solution submitted on Jul 16, 2014

1 player likes this solution

1 Comment