Cody

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

Solution 1601393

Submitted on 5 Aug 2018 by HH
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
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))

ans = 0 0 0 0 ans = 2 2 2 2 ans = 2 3 3 2 ans = 0 1 0 ans = 0 2 2 ans = 0 3 3 ans = 0 4 3

2   Pass
BW = eye(10); out_correct = 1:10; assert(isequal(findlastindim(BW,1),out_correct)) assert(isequal(findlastindim(BW,2),out_correct'))

ans = 1 0 0 0 0 0 0 0 0 0 ans = 1 2 0 0 0 0 0 0 0 0 ans = 1 2 3 0 0 0 0 0 0 0 ans = 1 2 3 4 0 0 0 0 0 0 ans = 1 2 3 4 5 0 0 0 0 0 ans = 1 2 3 4 5 6 0 0 0 0 ans = 1 2 3 4 5 6 7 0 0 0 ans = 1 2 3 4 5 6 7 8 0 0 ans = 1 2 3 4 5 6 7 8 9 0 ans = 1 2 3 4 5 6 7 8 9 10 ans = 1 0 0 0 0 0 0 0 0 0 ans = 1 2 0 0 0 0 0 0 0 0 ans = 1 2 3 0 0 0 0 0 0 0 ans = 1 2 3 4 0 0 0 0 0 0 ans = 1 2 3 4 5 0 0 0 0 0 ans = 1 2 3 4 5 6 0 0 0 0 ans = 1 2 3 4 5 6 7 0 0 0 ans = 1 2 3 4 5 6 7 8 0 0 ans = 1 2 3 4 5 6 7 8 9 0 ans = 1 2 3 4 5 6 7 8 9 10

3   Pass
BW = rand(10)>0.5; assert(isequal(findlastindim(BW,3),BW)) assert(isequal(findlastindim(BW,4),BW))

ans = 1 0 1 1 0 1 1 1 1 1 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 0 0 1 0 1 1 1 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 1 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 1 1 0 0 1 ans = 1 0 1 1 0 1 1 1 1 1 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 1 0 0 1 0 1 1 1 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 1 0 1 1 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 1 1 0 0 1

4   Pass
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))

ans = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 ans = 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