编写VB程序,实现矩阵转置,即将一个nⅹm的矩阵的行和列互换。

编写VB程序,实现矩阵转置,即将一个nⅹm的矩阵的行和列互换。例如:A矩阵及其转置矩阵B分别为:
A=[1 2 3
4 5 6]
B= [1 4
2 5
36 ]

Private Sub Form_Click()
Dim n, m, i, j, Matrix(), Cache() As Integer

n = CInt(InputBox("请输入矩阵行数N", "矩阵置换"))
m = CInt(InputBox("请输入矩阵列数M", "矩阵置换"))

ReDim Matrix(n - 1, m - 1)
ReDim Cache(m - 1, n - 1)

'录入
For i = 0 To n - 1
For j = 0 To m - 1
Matrix(i, j) = CInt(InputBox("请输入矩阵项(" & (i + 1) & "," & (j + 1) & ")", "矩阵置换"))
Next
Next

'转置
For i = 0 To n - 1
For j = 0 To m - 1
Cache(j, i) = Matrix(i, j)
Next
Next

'输出转置前的矩阵
Print "转置前的矩阵为:"
For i = 0 To n - 1
For j = 0 To m - 1
Print Matrix(i, j);
Next
Print
Next

Print

'输出转置后的矩阵
Print "转置后的矩阵为:"
For i = 0 To m - 1
For j = 0 To n - 1
Print Cache(i, j);
Next
Print
Next

End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-30
Dim a() As Integer, b() As Integer
Dim m As Integer, n As Integer, s As String
m = 3 '定义m值
n = 4 '定义n值
ReDim a(m * n - 1) '重定义数组a大小
For i = 1 To m * n
a(i - 1) = i '给数组a赋值
Next
s = ""
For i = 0 To UBound(a)
s = s & " " & a(i)
If (i + 1) Mod m = 0 Then
Print s '打印数组a矩形排列
s = ""
End If
Next
Print vbCrLf
ReDim b(UBound(a)) '重定义数组b大小
For i = 0 To UBound(b)
b(i) = a((i Mod n) * m + i \ n) '将a数组的矩形重新排列赋值给b数组
Next
s = ""
For i = 0 To UBound(b)
s = s & " " & b(i)
If (i + 1) Mod n = 0 Then
Print s '以矩形方式排列并且打印b数组
s = ""
End If
Next
第2个回答  推荐于2018-01-04
Private Sub Command1_Click()
Dim a#(), n1#, n2#, i#, j#
n1 = Text1.Text: n2 = Text2.Text
ReDim a(1 To n1, 1 To n2)
ReDim b(1 To n2, 1 To n1)
TextToArray Text3, a
For i = 1 To n1
For j = 1 To n2
b(j, i) = a(i, j)
Next
Next
ArrayToText b, Text4
End Sub
我也给难住了,在网上找了半天无果后,自己做出来了。。。。
虽然对你来说有点晚(n1,n2就是m,n),希望对别人有帮助吧本回答被网友采纳
第3个回答  2017-08-01
Private Sub Command1_Click()Randomize TimerDim n As Integer,m As IntegerDim arr1() As Integer,arr2() As IntegerDim i As Integer,j As Integern = InputBox("请输入矩阵的行数")m = InputBox("请输入矩阵的列...
第4个回答  2013-07-09
攻VB了?
相似回答