Need help to build a Matrix Identity

function matidentity(nsize as integer) as double(, )
dim i, j as integer
dim a2(-1, -1) as double
redim a2(nsize, nsize)
for i=0 to nsize
for j=0 to nsize
a2(i, j)=0.0
a2(i, i)=1.0
return a2()
end function

event action button

dim a(10, 10) as double
dim n, n1 as integer
n=Ubound(a(), 1)
system.debugLog ?
End Sub

// 1 0 0 0
// 0 1 0 0
// 0 0 1 0
// 0 0 0 1

What’s the question? Also, entering 4 will give you a 5x5 matrix (not the 4x4 you show at the bottom) because things are 0 based.

1 Like

The matrix 4x4 is shown what output must be.
My question is how to get this matrix, with 0 Based is default, thus n1=3, the output must be the matrix shown below ?

If I make n1=3, I do get that matrix. If you add this function you can view it.

Public Function matrixToString(matrix(, ) as double) as String
  dim matrixString as String = ""
  for row as Integer = 0 to UBound(matrix,1)
    matrixString = matrixString + str(matrix(row,0))
    for col as Integer = 1 to UBound(matrix, 2)
      matrixString = matrixString + " " + str(matrix(row, col))
    next col
    matrixString = matrixString + EndOfLine
  next row
  return matrixString
End Function

At the end of your button action event routine, use system.debugLog matrixToString(a()) to view it. You should verify that I didn’t get the row and column order backward (and add error checking, etc).

The argument must be a square matrix order “n”.
the identity matrix of size n is the n × n square matrix with ones on the main diagonal and zeros elsewhere.

I’m sorry, I guess I don’t understand what you’re asking for then. Your function “matidentity(3)” will return a “0 to 3 by 0 to 3” identity matrix, which is 4x4. If you’re looking for one that goes from 1 to 4, you could always make it a 5x5 matrix (by having n1=4) and just loop the rows and columns starting at 1 instead of 0. Good luck.

Ok Bill,
Thanks a lot.