Matrix creation in Rhinoscript

Hello everybody,
I am a new user of Rhinoscript. Currently, I am trying to write a script which will be used in my Diploma Project. But, I face too many problems doing it. That is why I am kindly asking for your help and advice.
So far I have developed a script which adds control points at the intersection of lines and identifies which points are connected to each other. But, the problem is:
I cannot make the programm display the resulting Matrix. When I write Rhino.Print Matrix(1,2) or any other cell of the resulting Matrix, the programm shows its value. But, I can’t make it to show the whole Matrix. (Rhino.Print Matrix(i,j) gives an error…)
Please help…

Option Explicit
'Script written by <insert name>
'Script copyrighted by <insert company name>
'Script version 4 октября 2016 г. 23:40:16
Call putpoints
Sub PutPoints()
	Dim i,spoint,epoint,N,PNTS,pts
	ReDim PNTS(0)
	N = 0
	Const CurveConst = 4
 
	Dim line : line = Rhino.ObjectsByType(CurveConst) 
	If IsNull(line) Then Rhino.Print("Nothing selected")
	If IsArray(line) Then
		For i=0 To Ubound(line)
			ReDim Preserve PNTS(N)
			PNTS(N) = Rhino.CurveStartPoint(line(i))
			N = N + 1                       
			ReDim Preserve PNTS(N)
			PNTS(N) = Rhino.CurveEndPoint(line(i))
			N = N + 1
		Next
	End If
	PNTS = Rhino.CullDuplicatePoints(PNTS)
	pts = Rhino.AddPoints(PNTS)
	For i=0 To ubound(PNTS)
		Rhino.addText i, PNTS(i), 1
	Next

	ReDim Lines(Ubound(line))
	Dim j,coord,dist,p
	For i=0 To ubound(line)
		Lines(i) = Array(Rhino.CurveStartPoint(line(i)), Rhino.CurveEndPoint(line(i)))
	Next
	Dim test_p
	Dim e:e = 1e-10
	Dim z
	z = Ubound(PNTS)
	ReDim Matrix(z,z)

	For i=0 To Ubound(PNTS)
		For j=0 To Ubound(line)
			dist = Rhino.Min(array(Rhino.Distance(PNTS(i), Rhino.CurveStartPoint(line(j))), Rhino.Distance(PNTS(i), Rhino.CurveEndPoint(line(j)))))
			If dist < e Then
				Rhino.AddText i, Rhino.CurveMidPoint(line(j)), 1.5
				Matrix(i, j) = 1
			End If
			If dist > e Then
				Matrix(i, j) = 0
			End If
			
		Next
	Next
	Rhino.Print Matrix(i, j)

End Sub

Just quick look and haven’t done rhino script in LONG time but if you simply move your Rhino.Print Matrix (i,j) to be inside your outer loop so will use those counters should work, think errors as by time it gets to that line one time i and j are both +1 more than the upper limits of your multi dimensional array.

Thank’s for your reply, ChrislARhino.
Finally, I got it by using
str = str & Matrix(i, j) command.
If anybody will meet the same problem of printing matrix, you can do it as follows.

Dim str, i,j
str = "" (1.Create empty str)
For i=0 To Ubound(smth)
  For j=0 To Ubound(smth)
    str = str & Matrix(i, j) (Creates first line of matrix)
  Next
  Rhino.Print str (Prints first line)
  str = "" (delete str)
Next

So that the program will print each row of matrix one by one, and finally the matrix will appear.

Here is a utility subroutine you can use to print the contents of a 2D array.

' Prints a 2-dimensional array to the command line
Sub PrintMatrix(arr)
	
  ' Declare local variables
  Dim i, j, cell, name, value
	
  ' Verify 'arr' is an array
  If Not IsArray(arr) Then Exit Sub
	
  ' Verify 'arr' is a 2-dimensional array
  On Error Resume Next
  Call UBound(arr, 2)
  If (Err.Number <> 0) Then Exit Sub
	
  ' Print the contents of the array
  For i = 0 To UBound(arr, 1)
    For j = 0 To UBound(arr, 2)
      cell = "Cell (" & CStr(i) & "," & CStr(j) & "): "
      name = "Type = " & TypeName(arr(i, j)) & ", "
      Select Case VarType(arr(i, j))
        Case 0 value = "Value = <empty>"
        Case 1 value = "Value = <null>"
        Case 9 value = "Value = <object>"
        Case 10 value = "Value = <error>"
        Case 12 value = "Value = <variant>"
        Case 13 value = "Value = <data>"
        Case 8192 value = "Value = <array>"
        Case Else value = "Value = " & CStr(arr(i, j))
      End Select
      Call Rhino.Print(cell & name & value)			
    Next
  Next
	
End Sub