new*_*his 4 arrays matlab sparse-matrix julia
我收到了一个 Matlab 文件,其中应该包含一些矩阵。我在 Matlab 在线上打开它,它看起来像一个 Excel 工作表,其中每个单元格都有一个类变量double,并被称为sparse double. 如果我尝试打印它,它会给出一个坐标列表,后跟 1。例如:
(100,1) 1\n(123,132) 1\nRun Code Online (Sandbox Code Playgroud)\n我正在使用的矩阵只能有 0,1 作为元素,所以我假设所有其他坐标都为零。但是,我不知道如何将其显示为矩阵或以某种方式将其作为数组导入 Julia。我对 Matlab 毫无了解,我什至不想在 Matlab 上工作,因为我的程序的其余部分都是在 Julia 中完成的。
\n编辑:正如评论所建议的,我只是留下我正在使用的代码以便尝试导入它。在Matlab程序中,我有一个“单元”格式的单个变量,其大小为1x10,称为modmat. 其中每个都包含 1 个266x266 sparse double矩阵,我将其访问为等modmat{1}。modmat{2}
MATLAB:
\nwritematrix(modmat{1},"Mat1.txt")\nRun Code Online (Sandbox Code Playgroud)\n在朱莉娅:
\n> using DelimitedFiles\n> M1 = open(readdlm,"Mat1.txt")\nRun Code Online (Sandbox Code Playgroud)\n输出是一个266\xc3\x971 Matrix{Any}:变量
我建议使用MAT.jlmat包来安全有效地读取文件。看起来它也可以读取稀疏矩阵,甚至可以一次性读取整个元胞数组。
为了完整起见(如果您由于某种原因无法执行上述操作),这里介绍了如何读取包含以下格式行的文件
\n(100,1) 1\n(123,132) 1\nRun Code Online (Sandbox Code Playgroud)\n:
\nfunction readsparsemat(io::IO)\n linere = r"^\\((\\d+),(\\d+)\\) # coordinates\n \\s+ # some number of spaces\n 1$ # 1 at the end of the line\n "x # extended regex complete\n\n matches = match.(linere, readlines(io))\n coords = [parse.(Int, (m[1], m[2])) for m in matches]\n\n sparse(first.(coords), last.(coords), true)\n \nend\nRun Code Online (Sandbox Code Playgroud)\njulia> readsparsemat(IOBuffer("(10,1) 1\n (12,13) 1\n ")) # test with an IOBuffer\n12\xc3\x9713 SparseMatrixCSC{Bool, Int64} with 2 stored entries:\n...\n\njulia> open(readsparsemat, "matfilename") #actual usage\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
727 次 |
| 最近记录: |