Does this help?
%%
matrix = magic(10);
matrix = triu( matrix,-4 );
matrix = tril( matrix, 4 )
%% This is better, it only overwrites the specific diagonals
matrix = magic(10);
for k = [5,7,9]
isdiag = diag( true(10-k,1), k );
matrix( isdiag ) = 0;
isdiag = diag( true(10-k,1), -k );
matrix( isdiag ) = 0;
end
matrix
I was fooled by your example and used a 10x10 matrix in my demos. Your title says MxN matrix.