Question: Using if/else statements to fill an empty matrix?

I am trying to use a do loop with if/else statements to create a 5x5 unit matrix. I made an empty array. Converted it to a matrix. Then made a do loop where I was trying to get the matrix elements where i=j to be 1 and all else to be 0. It didn't spit out a matrix.

Any advice? I assume I must have missed a small detail in syntax.


 

with(LinearAlgebra):

````

U := array(1 .. 5, 1 .. 5);

array( 1 .. 5, 1 .. 5, [ ] )

(1)

NULL

for i to 5 do for j to 5 do if i = j then U[i, j] := 1 else U[i, j] := 0 end if end do end do

U;

U

(2)

``


 

Download fail_unit_matrix_loop.mw

By the way, I am open to completely different methods, also! I was just trying to use loops to do it rather than inbuilt commands.

Please Wait...