% Create a 3D matrix with consecutive elements X=5; Y=7; Z=11; disp('Creating 3D test Matrix'); P=reshape(1:(X*Y*Z),[X Y Z]); assert(size(P),[X Y Z]); assert(min(P(:)),1); assert(max(P(:)),X*Y*Z); disp('Now lets take some slices along the first dimension'); for i=1:X % printf('*Creating 2D slice along first dimension at offset %d\n',i); A=P(i,:,:); assert(size(A),[1 Y Z]); A=reshape(A,[Y Z]); for j=1:Y % printf('**Creating 1D slice along first dimension at offset %d\n',j); A1=A(j,:); assert((A1-((j-1)*X)- i)/(X*Y),1*(0:(Z-1))); end for k=1:Z % printf('**Creating 1D slice along second dimension at offset %d\n',k); A2=A(:,k); assert(reshape((A2-((k-1)*(X*Y))- i)/X,[1 Y]),1*(0:(Y-1))); end end disp('Now lets take some slices along the second dimension'); for j=1:Y % printf('*Creating 2D slice along second dimension at offset %d\n',j); A=P(:,j,:); assert(size(A),[X 1 Z]); A=reshape(A,[X Z]); for i=1:X % printf('**Creating 1D slice along first dimension at offset %d\n',i); A3=A(i,:); assert((A3-((j-1)*X)- i)/(X*Y),1*(0:(Z-1))); end for k=1:Z % printf('**Creating 1D slice along second dimension at offset %d\n',k); A4=A(:,k); assert(reshape((A4-((k-1)*X*Y)- ((j-1)*X) - 1),[1 X]),1*(0:(X-1))); end end disp('Now lets take some slices along the third dimension'); for k=1:Z % printf('*Creating 2D slice along third dimension at offset %d\n',k); A=P(:,:,k); assert(size(A),[X Y]); for i=1:X % printf('**Creating 1D slice along first dimension at offset %d\n',i); A5=A(i,:); assert((A5-((k-1)*X*Y)- i)/X,1*(0:(Y-1))); end for j=1:Y % printf('**Creating 1D slice along second dimension at offset %d\n',j); A6=A(:,j); assert(reshape((A6-((j-1)*X)- ((k-1)*X*Y) - 1),[1 X]),1*(0:(X-1))); end end disp('Now lets test some simple 1D cases'); B=1:100; assert(size(B),[1 100]); printf('*Testing 1D middle slice\n'); B1=B(19:65); assert(size(B1),[1 65-19+1]); assert(min(B1),19); assert(max(B1),65); assert(B1,1*(19:65)); printf('*Testing 1D strided slice\n'); B2=B(1:2:100); assert(size(B2),[1 50]); assert(all(mod(B2,2) == 1)); assert(sum(B2), 50 * 50 );