Code covered by the BSD License  

Highlights from
SLM

from SLM by Jos (10584)
special linear matrix

slm(varargin)
function A = slm(varargin)

% SLM - Special Linear Matrix
%    M = SLM(..) returns a matrix M in which each element is the same as its linear index
%    (so that "all(find(M)==M(:))" is true).
%
%    SLM(N) is an N-by-N matrix containing [1:(N*N)] as its elements.
%    SLM(M,N) or SLM([M,N]) is an M-by-N matrix containing [1:(M*N)].
%    SLM(M,N,P,...) or SLM([M N P ...]) is an M-by-N-by-P-by-...
%      array containing 1:(M*N*P*...).
%    SLM(SIZE(A)) is the same size as A containing [1:numel(A)] as its elements.
%
%    Example:
%    slm(3,4) will give the matrix
%         1     4     7    10
%         2     5     8    11
%         3     6     9    12
%
%    Such a matrix can be useful for testing matrix operations.
% 
%    See also MAGIC, EYE, ONES, ZEROS, GALLERY

% for Matlab R13
% version 1.0 (feb 2006)
% (c) Jos van der Geest
% email: jos@jasen.nl

% Use ONES to check the arguments.
% If ONES is OK, we are also ...
try
    A = zeros(varargin{:}) ;
catch
    % show the error, but adapt so that it does not show the call to zeros.
    a = lasterror ;
    error(strrep(a.message(23:end),'zeros',mfilename)) ;
end

% Create the matrix, very simple indeed ;-)
A(:) = 1:numel(A) ;

    
    

Contact us at files@mathworks.com