首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

求 将已知行数和列数的二维矩阵 存为一维形式的代码,该如何解决

2012-05-21 
求 将已知行数和列数的二维矩阵 存为一维形式的代码跪求 已知行数m和列数n的二维矩阵 存为一维形式的代码

求 将已知行数和列数的二维矩阵 存为一维形式的代码
跪求 已知行数m和列数n的二维矩阵 存为一维形式的代码转换代码 

例 如下

如 已知二维数组 行数为3 列数为4

15 12 23 45
11 123 15 19
12 76 5 9

转化成以下形式
[15,12,23,45;11,123,15,19;12,76,5,9] 

即 同行用","隔开 不同行用";"隔开
整个数组首尾用"[]" 标注

即要有二维转成一维的 也要有将一维恢复成二维的 代码

[解决办法]
用这2个函数就行了
一个从矩阵转为字符串,一个从字符串转回去,
没有exception判断,如果需要你自己加上

VB.NET code
Function mat2str(ByVal mat As Integer(,)) As String        mat2str = "["        For row = LBound(mat, 1) To UBound(mat, 1)            For col = LBound(mat, 2) To UBound(mat, 2)                mat2str += CStr(mat(row, col))                mat2str += IIf(col = UBound(mat, 2), "", ",")            Next            mat2str += IIf(row = UBound(mat, 1), "]", ";")        Next    End Function    Function str2mat(ByVal str As String) As Integer(,)        Dim result As Integer(,)        Dim rows As String(), cols As String()        Dim row As Integer, col As Integer        str = Replace(str, "[", "")        str = Replace(str, "]", "")        rows = Split(str, ";")        row = UBound(rows) - LBound(rows) + 1        cols = Split(rows(0), ",")        col = UBound(cols) - LBound(cols) + 1        ReDim result(row, col)        For row = LBound(rows) To UBound(rows)            cols = Split(rows(row), ",")            For col = LBound(cols) To UBound(cols)                result(row, col) = CInt(cols(col))            Next        Next        str2mat = result    End Function 

热点排行