Cody

Problem 1972. Convert matrix to 3D array of triangular matrices

Solution 360752

Submitted on 27 Nov 2013 by Michael Ryan
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
%% x = 1:100; y_correct = shiftdim(x,-1); assert(isequal(mat2triu3(x),y_correct))

2   Pass
%% x = reshape(1:15,3,[]); y_correct(:,:,1) = [1 2;0 3]; y_correct(:,:,2) = [4 5;0 6]; y_correct(:,:,3) = [7 8;0 9]; y_correct(:,:,4) = [10 11;0 12]; y_correct(:,:,5) = [13 14;0 15]; assert(isequal(mat2triu3(x),y_correct))

3   Pass
%% x = reshape(1:18,3,[])'; y_correct(:,:,1) = [1 4 10; 0 7 13; 0 0 16]; y_correct(:,:,2) = [2 5 11; 0 8 14; 0 0 17]; y_correct(:,:,3) = [3 6 12; 0 9 15; 0 0 18]; assert(isequal(mat2triu3(x),y_correct))

4   Pass
%% x = randi(50,sum(1:100),22); y = mat2triu3(x); mask = (y~=0); xb = reshape(y(mask),[],size(y,3)); assert(isequal(size(y),[100 100 22])) assert(isequal(x,xb))