0% found this document useful (0 votes)
45 views

Coding Matriks

This document contains VBA code that defines several subroutines for performing matrix operations in Excel, including: 1. Creating random matrices A and B of size n x n and storing them in the worksheet. 2. Adding matrices A and B and storing the result in matrix C. 3. Multiplying matrices A and B by a scalar value k and storing the results. 4. Taking the transpose of matrices A and B and storing the results. 5. Multiplying matrices A and B and storing the results. 6. Computing the determinant and inverse of matrices A and B.

Uploaded by

mia indriyati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Coding Matriks

This document contains VBA code that defines several subroutines for performing matrix operations in Excel, including: 1. Creating random matrices A and B of size n x n and storing them in the worksheet. 2. Adding matrices A and B and storing the result in matrix C. 3. Multiplying matrices A and B by a scalar value k and storing the results. 4. Taking the transpose of matrices A and B and storing the results. 5. Multiplying matrices A and B and storing the results. 6. Computing the determinant and inverse of matrices A and B.

Uploaded by

mia indriyati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Sub setMatriks()

Dim A() As Double


Dim B() As Double

Range([B4], [z100]).ClearContents

n = [C2]

'Membuat Matriks A
ReDim A(1 To n, 1 To n)

Cells(4, 3) = "A = "


For i = 1 To n
For j = 1 To n

A(i, j) = Int(9 * Rnd + 1) 'Membangkitkan Bil. Acak 1-9


Cells(4 + i, 2 + j) = A(i, j)

Next j
Next i

'Membuat Matriks B
ReDim B(1 To n, 1 To n)
Cells(4, 4 + n) = "B = "
For i = 1 To n
For j = 1 To n

B(i, j) = Int(9 * Rnd + 1) 'Membangkitkan Bil. Acak 1-9


Cells(4 + i, 3 + n + j) = B(i, j)

Next j
Next i
End Sub

Sub hapusHasil()
Range([B4], [z100]).ClearContents
End Sub

Sub TambahMatriks()

Dim A() As Variant


Dim B() As Variant
Dim C() As Double

n = [C2]
'mengambil nilai Matriks A dan B yang ada di Sheet
A = Range(Cells(5, 3), Cells(4 + n, 2 + n)).Value
B = Range(Cells(5, 4 + n), Cells(4 + n, 2 * n + 3)).Value

Range(Cells(6 + n, 2), [z100]).ClearContents


Cells(6 + n, 2) = " A + B "
ReDim C(1 To n, 1 To n)
For i = 1 To n
For j = 1 To n
C(i, j) = A(i, j) + B(i, j)
Cells(n + i + 6, j + 2) = C(i, j)

Next j
Next i

End Sub

Sub perkalianSkalarK()
Dim A() As Variant
Dim B() As Variant
Dim KA() As Double
Dim KB() As Double
Dim k As Double

n = [C2]
'mengambil nilai Matriks A dan B yang ada di Sheet
A = Range(Cells(5, 3), Cells(4 + n, 2 + n)).Value
B = Range(Cells(5, 4 + n), Cells(4 + n, 2 * n + 3)).Value

Range(Cells(6 + n, 2), [z100]).ClearContents


k = InputBox("Tuliskan Nilai K=", " Tulis Nilai Skalar K")

Cells(6 + n, 2) = k & " * A dan " & k & " * B"


ReDim KA(1 To n, 1 To n)
For i = 1 To n
For j = 1 To n
KA(i, j) = k * A(i, j)
Cells(n + i + 6, j + 2) = KA(i, j)

Next j
Next i

ReDim KB(1 To n, 1 To n)
For i = 1 To n
For j = 1 To n
KB(i, j) = k * B(i, j)
Cells(n + i + 6, j + 3 + n) = KB(i, j)

Next j
Next i

End Sub

Sub TransposeMatriks()
Dim A() As Variant
Dim B() As Variant
Dim TA() As Double
Dim TB() As Double

n = [C2]
'mengambil nilai Matriks A dan B yang ada di Sheet
A = Range(Cells(5, 3), Cells(4 + n, 2 + n)).Value
B = Range(Cells(5, 4 + n), Cells(4 + n, 2 * n + 3)).Value

Range(Cells(6 + n, 2), [z100]).ClearContents

Cells(6 + n, 2) = " Transpose Matriks : "

ReDim TA(1 To n, 1 To n)
For i = 1 To n
For j = 1 To n
TA(i, j) = A(j, i)
Cells(n + i + 6, j + 2) = TA(i, j)

Next j
Next i

ReDim TB(1 To n, 1 To n)
For i = 1 To n
For j = 1 To n
TB(i, j) = B(j, i)
Cells(n + i + 6, j + 3 + n) = TB(i, j)

Next j
Next i
End Sub
Sub TransposeMatriks2()

Dim A() As Variant


Dim B() As Variant
Dim TA() As Variant
Dim TB() As Variant

n = [C2]
'mengambil nilai Matriks A dan B yang ada di Sheet
A = Range(Cells(5, 3), Cells(4 + n, 2 + n)).Value
B = Range(Cells(5, 4 + n), Cells(4 + n, 2 * n + 3)).Value

Range(Cells(6 + n, 2), [z100]).ClearContents

Cells(6 + n, 2) = " Transpose Matriks : "

TA = WorksheetFunction.Transpose(A)
TB = WorksheetFunction.Transpose(B)
For i = 1 To n
For j = 1 To n

Cells(n + i + 6, j + 2) = TA(i, j)
Cells(n + i + 6, j + 3 + n) = TB(i, j)
Next j
Next i
End Sub
Sub perkalianMatriks()

Dim A() As Variant


Dim B() As Variant
Dim AB() As Variant
Dim BA() As Variant

n = [C2]
'mengambil nilai Matriks A dan B yang ada di Sheet
A = Range(Cells(5, 3), Cells(4 + n, 2 + n)).Value
B = Range(Cells(5, 4 + n), Cells(4 + n, 2 * n + 3)).Value

Range(Cells(6 + n, 2), [z100]).ClearContents

Cells(6 + n, 2) = " Perkalian Matriks : "

AB = WorksheetFunction.MMult(A, B)
BA = WorksheetFunction.MMult(B, A)
For i = 1 To n
For j = 1 To n

Cells(n + i + 6, j + 2) = AB(i, j)
Cells(n + i + 6, j + 3 + n) = BA(i, j)
Next j
Next i
End Sub

Sub DeterminanMatriks()

Dim A() As Variant


Dim B() As Variant
Dim DA As Double
Dim DB As Double

n = [C2]
'mengambil nilai Matriks A dan B yang ada di Sheet
A = Range(Cells(5, 3), Cells(4 + n, 2 + n)).Value
B = Range(Cells(5, 4 + n), Cells(4 + n, 2 * n + 3)).Value

Range(Cells(6 + n, 2), [z100]).ClearContents

Cells(6 + n, 2) = " Determinan Matriks : "

DA = WorksheetFunction.MDeterm(A)
DB = WorksheetFunction.MDeterm(B)

Cells(n + 7, 3) = DA
Cells(n + 7, 4 + n) = DB

End Sub
Sub InversMatriks()

Dim A() As Variant


Dim B() As Variant
Dim IA() As Variant
Dim IB() As Variant

n = [C2]
'mengambil nilai Matriks A dan B yang ada di Sheet
A = Range(Cells(5, 3), Cells(4 + n, 2 + n)).Value
B = Range(Cells(5, 4 + n), Cells(4 + n, 2 * n + 3)).Value

Range(Cells(6 + n, 2), [z100]).ClearContents

Cells(6 + n, 2) = " Invers Matriks : "

IA = WorksheetFunction.MInverse(A)
IB = WorksheetFunction.MInverse(B)
For i = 1 To n
For j = 1 To n

Cells(n + i + 6, j + 2) = IA(i, j)
Cells(n + i + 6, j + 3 + n) = IB(i, j)
Next j
Next i
End Sub

You might also like