【问题标题】:Working with matrices using LibreOffice Basic (LibreOffice Calc)使用 LibreOffice Basic (LibreOffice Calc) 处理矩阵
【发布时间】:2017-07-07 08:28:12
【问题描述】:

我将在 4 × 6(代码中为 m×n)矩阵的每个单元格上使用以下公式来获得归一化矩阵:

Calc 中的矩阵为:

我在 LibreOffice 中使用以下基本代码:

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 

我想让归一化矩阵出现在原始矩阵的右侧。但什么也没有出现。我究竟做错了什么?

【问题讨论】:

    标签: matrix libreoffice-calc libreoffice-basic


    【解决方案1】:

    代码使用nm 作为参数,但随后声明它们,破坏了值。要修复,请删除以下两行。为了便于阅读,请将这些 cmets 移到 Sub Normalize 附近。

    Dim m 'maximum row index 
    Dim n 'maximum column index
    

    要使用 Basic IDE 调试器查找此类问题,请按工具栏中的 Breakpoint On/Off 设置几个断点,并启用 Watch。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多