Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 46132

Submitted on 20 Feb 2012 by Alan Chalker

Correct

32Size
Leading solution size is 28.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%% Test the examples
BW = [0 0 0 0; 1 1 1 1; 0 1 1 0];
out_correct = [2 3 3 2];
assert(isequal(findlastindim(BW,1),out_correct))
out_correct = [0; 4; 3];
assert(isequal(findlastindim(BW,2),out_correct))

                    
2
Pass
 
%% Test some different matrices
BW = eye(10);
out_correct = 1:10;
assert(isequal(findlastindim(BW,1),out_correct))
assert(isequal(findlastindim(BW,2),out_correct'))

                    
3
Pass
 
%% Test some different dimensions (and prevent hard-coded solutions)
BW = rand(10)>0.5;
assert(isequal(findlastindim(BW,3),BW))
assert(isequal(findlastindim(BW,4),BW))

                    
4
Pass
 
%% Test something in the 3rd dimension that is small enough to visualise.
BW = cat(3, eye(5), fliplr(eye(5)));
out_correct = [1     0     0     0     2
     0     1     0     2     0
     0     0     2     0     0
     0     2     0     1     0
     2     0     0     0     1];
assert(isequal(findlastindim(BW,3),out_correct))