我有一个关于vb矩阵的问题。
我想把一些数据放入一个3阶矩阵中,但是不止一个矩阵,我想要用循环的方式把这些数据一一搁入矩阵中。矩阵中的值各不相同...
我想把一些数据放入一个3阶矩阵中,但是不止一个矩阵,我想要用循环的方式把这些数据一一搁入矩阵中。矩阵中的值各不相同
展开
2个回答
展开全部
这是我原来回答一个问题的程序,希望可以给你点帮助
Sub Sub1()
Dim M(1 To 4, 1 To 4) As Integer
Dim i, j As Long
Dim s As String
' 为矩阵M赋初值
For i = 1 To 4
For j = 1 To 4
M(i, j) = j + (i - 1) * 4
Next
Next
' 输出矩阵
Me.Print "+++++++++初始矩阵+++++++"
For i = 1 To 4
For j = 1 To 4
s = s & vbTab & M(i, j)
Next
Me.Print s
s = ""
Next
' 矩阵转置
' 输出转置后的矩阵
Me.Print vbCrLf & "++++++++++++转置后矩阵+++++++++++"
For j = 1 To 4
For i = 1 To 4
s = s & vbTab & M(i, j)
Next
Me.Print s
s = ""
Next
End Sub
Sub Sub1()
Dim M(1 To 4, 1 To 4) As Integer
Dim i, j As Long
Dim s As String
' 为矩阵M赋初值
For i = 1 To 4
For j = 1 To 4
M(i, j) = j + (i - 1) * 4
Next
Next
' 输出矩阵
Me.Print "+++++++++初始矩阵+++++++"
For i = 1 To 4
For j = 1 To 4
s = s & vbTab & M(i, j)
Next
Me.Print s
s = ""
Next
' 矩阵转置
' 输出转置后的矩阵
Me.Print vbCrLf & "++++++++++++转置后矩阵+++++++++++"
For j = 1 To 4
For i = 1 To 4
s = s & vbTab & M(i, j)
Next
Me.Print s
s = ""
Next
End Sub
展开全部
'新建一个工程,在form中添加一个按钮,按钮中写上以下代码即可
Private Sub Command1_Click()
Dim n As Long, m As Long
Dim i As Long, j As Long
Dim x As Long, y As Long, z As Long
Dim a() As Long
n = Val(InputBox("输入N", "输入N", 1))
m = Val(InputBox("输入M", "输入M", 1))
If n < 1 And m < 1 Then Exit Sub
ReDim a(n - 1, m - 1)
Randomize Timer
For i = 0 To n - 1
For j = 0 To m - 1
a(i, j) = Int(Rnd() * (n * m))
If z < a(i, j) Then
x = i + 1
y = j + 1
z = a(i, j)
End If
Next
Next
Cls
For i = 0 To n - 1
For j = 0 To m - 1
Print Space(5 - Len(CStr(a(i, j)))) & a(i, j);
Next
Print
Next
Print "最大值:" & z
Print "行:" & x
Print "列:" & y
End Sub
Private Sub Command1_Click()
Dim n As Long, m As Long
Dim i As Long, j As Long
Dim x As Long, y As Long, z As Long
Dim a() As Long
n = Val(InputBox("输入N", "输入N", 1))
m = Val(InputBox("输入M", "输入M", 1))
If n < 1 And m < 1 Then Exit Sub
ReDim a(n - 1, m - 1)
Randomize Timer
For i = 0 To n - 1
For j = 0 To m - 1
a(i, j) = Int(Rnd() * (n * m))
If z < a(i, j) Then
x = i + 1
y = j + 1
z = a(i, j)
End If
Next
Next
Cls
For i = 0 To n - 1
For j = 0 To m - 1
Print Space(5 - Len(CStr(a(i, j)))) & a(i, j);
Next
Next
Print "最大值:" & z
Print "行:" & x
Print "列:" & y
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询