Task

Build an   identity matrix   of a size known at run-time.



An identity matrix is a square matrix of size n × n,

where the diagonal elements are all 1s (ones),

and all the other elements are all 0s (zeroes).







I

n


=


[



1


0


0


⋯


0




0


1


0


⋯


0




0


0


1


⋯


0




⋮


⋮


⋮


⋱


⋮




0


0


0


⋯


1



]




{\displaystyle I_{n}={\begin{bmatrix}1&0&0&\cdots &0\\0&1&0&\cdots &0\\0&0&1&\cdots &0\\\vdots &\vdots &\vdots &\ddots &\vdots \\0&0&0&\cdots &1\\\end{bmatrix}}}





Related tasks

   Spiral matrix
   Zig-zag matrix 
   Ulam_spiral_(for_primes)


begin
    % set m to an identity matrix of size s                                  %
    procedure makeIdentity( real    array m ( *, * )
                          ; integer value s
                          ) ;
        for i := 1 until s do begin
            for j := 1 until s do m( i, j ) := 0.0;
            m( i, i ) := 1.0
        end makeIdentity ;
 
    % test the makeIdentity procedure                                        %
    begin
        real array id5( 1 :: 5, 1 :: 5 );
        makeIdentity( id5, 5 );
        r_format := "A"; r_w := 6; r_d := 1; % set output format for reals   %
        for i := 1 until 5 do begin
            write();
            for j := 1 until 5 do writeon( id5( i, j ) )
        end for_i ;
    end text
 
end.