Home > Net >  Finding and printing the lowest value number and its indexes from a newly generated matrix
Finding and printing the lowest value number and its indexes from a newly generated matrix

Time:12-19

The task I am doing requires to read and print a matrix from txt file, then create a new matrix B whose elements are the average of the row and column from A matrix, and then find the lowest valued number in the B matrix, print it and print its indexes (If the element "1" is the lowest and it is in the 2nd row and 3rd column, there should be printed below the matrix B "The lowest element is 1 with indexes 2;3". For example the element B23 should be the average of the sum of the elements of row 2 and column 3 of matrix A. There is a short matrix example in the code below.

The input for the A matrix is coming from txt file, where on the first row are m and n (rows and columns) and below them is the actual matirx. Example:

Thank you in advance!

Here is the code:

`

" Example for input
3 3
2 9 8
4 2 5
5 2 3

Expected answear:
Matrix A        
2.00    9.00    8.00
4.00    2.00    5.00
5.00    2.00    3.00
Matrix B
15.00   16.00   17.50
11.00   12.00   13.50
10.50   11.50   13.00  

The lowest element is 10.50 with indexes 3,1.

Option Explicit

Sub Matrix()

    Dim m As Integer, n As Integer, A() As Single, _
        MaxA As Single, r_Max As Integer

    Call InputMatrix(m, n, A)
    Call NewMatrixB(A, m)
 
End Sub


Sub InputMatrix(m As Integer, n As Integer, A() As Single)
    Dim i As Integer, j As Integer
    Dim FName As String

    FName = InputBox("Vuvedete ime na fail s vhodni danni", _
        "matrix", "H:\School\matrix.txt")
 
    If Dir(FName) = "" Then
        MsgBox ("Failut " & FName & " ne e nameren!")
        Stop
 
    End If

    Close #1
 
    Open FName For Input As #1
    Input #1, m, n
    ReDim A(m, n)
    For i = 1 To m
        For j = 1 To n
            Input #1, A(i, j)
        Next j
    Next i

    Close #1

    Worksheets("Sheet1").Activate
    Cells.Clear
    Call OutMatrix(m, n, A, 1, "Matrix A")

End Sub


Sub OutMatrix(m As Integer, n As Integer, A() As Single, _
    r As Integer, title As String)

    Dim i As Integer, j As Integer

    With Cells(r, 1)
        .Value = title
        .Font.Size = 14
        .Font.Bold = True
    End With

    For i = 1 To m
        For j = 1 To n
            Cells(r   i, j).Value = A(i, j)
            Cells(r   i, j).NumberFormat = "0.00"
        Next j
    Next i

End Sub

Sub NewMatrixB(Data As Variant, m As Integer)

    Dim X As Variant
    X = Data
    
    Dim numRows As Long
    Dim numCols As Long
    
    numRows = UBound(X, 1)
    numCols = UBound(X, 2)
    
    ReDim rowSum(1 To numCols) As Double
    ReDim colSum(1 To numRows) As Double
    
    Dim r As Long
    Dim c As Long
    For r = 1 To numRows
        For c = 1 To numCols
            rowSum(c) = rowSum(c)   X(r, c)
            colSum(r) = colSum(r)   X(r, c)
        Next
    Next
   
    ReDim B(1 To numRows, 1 To numCols) As Double
    For r = 1 To numRows
        For c = 1 To numCols
            B(r, c) = (rowSum(c)   colSum(r)) / 2
        Next
    Next
    
    With Cells(m   3, 1)
        .Value = "Matrix B"
        .Font.Size = 14
        .Font.Bold = True
           
    End With
    
    Cells(m   4, 1).Resize(numRows, numCols) = B
    
    Worksheets("Sheet1").Range("A1:X100").NumberFormat = "0.00"
    
    Dim Min As Integer
   
End Sub


Sub Minimum(m As Integer, Matrixxx As Single)

    MsgBox Application.Min(Matrixxx)

End Sub

As far as I went, my code is up to the new matrix B and printing it, but I have trouble finding the new one and making it a type, where I can get the indexes as well. I also do have trouble with the syntacsis, Have been coding 5years ago in java.

CodePudding user response:

If you need to return a value then define a function. To return more than one value use an array.

Option Explicit
Sub Process_Matrix()

    ' define matrices
    Dim A As Variant, B As Variant
    
    A = InputMatrix("matrix.txt") '"H:\School\matrix.txt")
    Call OutMatrix("Matrix A", Sheet1.Cells(1, 1), A)
    
    B = NewMatrixB(A)
    Call OutMatrix("Matrix B", Sheet1.Cells(12, 1), B)

    Dim idx, msg As String
    idx = getMin(B)
    msg = "Min = " & B(idx(0), idx(1)) & " at B(" & idx(0) & "," & idx(1) & ")"
    
    MsgBox msg, vbInformation
 
End Sub

Function getMin(ByRef X) As Variant

    Dim i As Long, j As Long, m As Double, ar(0 To 1) As Long
    m = X(1, 1)
    ar(0) = 1
    ar(1) = 1
    For i = 1 To UBound(X)
        For j = 1 To UBound(X, 2)
            If X(i, j) < m Then
                m = X(i, j)
                ar(0) = i
                ar(1) = j
            End If
        Next
    Next
    getMin = ar
End Function


Function InputMatrix(Fname As String) As Variant
         
    Dim i As Long, j As Long, m As Long, n As Long, A() As Single
    Fname = InputBox("Vuvedete ime na fail s vhodni danni", _
    "matrix", Fname)
     
    If Dir(Fname) = "" Then
        MsgBox "Failut " & Fname & " ne e nameren!", vbCritical
        Stop
     
    End If
    Close #1
     
    Open Fname For Input As #1
    Input #1, m, n
    
    ReDim A(1 To m, 1 To n)
    For i = 1 To m
        For j = 1 To n
            Input #1, A(i, j)
        Next j
    Next i
    Close #1
    InputMatrix = A
      
End Function
    
Sub OutMatrix(title As String, rng As Range, ByRef X)
    
    With rng
        .Value = title
        .Font.Size = 14
        .Font.Bold = True
        With .Offset(1, 0).Resize(UBound(X), UBound(X, 2))
            .Value = X
            .NumberFormat = "0.00"
        End With
    End With
     
End Sub
     
Function NewMatrixB(ByRef X) As Variant
    
    Dim B, rowSum, colSum
    Dim numRows As Long, numCols As Long, r As Long, c As Long
    
    numRows = UBound(X, 1)
    numCols = UBound(X, 2)
    ReDim rowSum(1 To numRows) As Double
    ReDim colSum(1 To numCols) As Double
        
    For r = 1 To numRows
        For c = 1 To numCols
            rowSum(c) = rowSum(c)   X(r, c)
            colSum(r) = colSum(r)   X(r, c)
        Next
    Next
            
    ReDim B(1 To numRows, 1 To numCols) As Double
    For r = 1 To numRows
        For c = 1 To numCols
            B(r, c) = (rowSum(c)   colSum(r)) / 2
        Next
    Next
    NewMatrixB = B
       
End Function
  • Related