title: Working with matrices using LibreOffice Basic (LibreOffice Calc)
body: <p>I am going to use the following formula on every cell of a <strong>4 × 6</strong> (m×n in the code) matrix to get the normalized matrix:<br>
  <img src="https://i.stack.imgur.com/whsYj.gif" alt="\pi_{ij}=\frac{x_{ij}}{\sqrt{\Sigma_{i=1}^m x_{ij}^2}}"></p>
  
  <p>The matrix in Calc is:<br>
  <a href="https://i.stack.imgur.com/DZqj5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DZqj5.png" alt="enter image description here"></a>  </p>
  
  <p>I use the following Basic code in LibreOffice:  </p>
  
  <pre><code>REM  *****  BASIC  *****
  
  Sub Main
  
  Normalize(5,3)
  
  End Sub
  
  
  
  Sub Normalize (ByVal n As Integer,ByVal m As Integer)
  
  Dim Doc As Object
  Dim Sheet As Object
  Dim SrcCell 'Cell in the source matrix 
  Dim TargetCell 'Cell in the target matrix where normalized values are saved 
  Dim TempCell As Object 
  
  Dim I 'index 
  Dim J 'index 
  Dim JJ 'inner index 
  Dim Sum 'Sigma x_ij^2 (j=0 to m)
  Dim m 'maximum row index 
  Dim n 'maximum column index 
  
  
  Doc = ThisComponent
  Sheet = Doc.Sheets(0)
  
  
  
  
  For I = 0 to n  'traverse columns 
      For J=0 to m 'traverse rows 
          SrcCell = Sheet.getCellByPosition(I,J)
          'Now apply the normalization formula for this cell 
          'Run a new loop to run formula on this cell 
          Sum = 0 'Reset Sum to 0
          For JJ=0 to m 
              TempCell = Sheet.getCellByPosition(I,JJ)
              Sum = Sum + (TempCell.Value^2)
          Next 
          TargetCell = Sheet.getCellByPosition(I+n+1,J) 'Place the normalized cells in a new matrix cell, n+1 cells away
  
          'Put the sum in the formula 
          TargetCell.Value = SrcCell.Value/Sqr(Sum)
  
      Next 
  
  
  Next 
  
  End Sub 
  </code></pre>
  
  <p>I want am going to have the normalized matrix appear on the right side of the original one. But nothing appears. What am I doing wrong? </p>