r - 将数组从 matlab 导入 R

标签 r matlab file-io matrix mat-file

我确信这是一个简单的问题,但我一直没能找到明显的解决方案。我在 matlab 中生成了一系列模型输出数组文件(dim 180、360、12),我需要在 R 中打开这些文件。

我尝试使用 R.matlab 包,仅使用 readMat 命令,这会产生一个列表对象。尝试将此列表写入矩阵会导致内存分配错误。

我试过取消列出,但这也没有帮助。

如何打开这些 matlab 矩阵文件并在 R 中编写为矩阵?有什么想法吗?


到目前为止阅读的 matlab 代码很简单:

> data<-readMat("filename")
> typeof(data)
[1] "list"
> str(data)
List of 1
 $ pco2: num [1:180, 1:360, 1:12] NaN NaN NaN NaN NaN ...
 - attr(*, "header")=List of 3
 ..$ description: chr "MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Thu Jul 26 10:36:42 2012 "
 ..$ version : chr "5"
 ..$ endian : chr "little

最佳答案

请仔细检查您在 MATLAB 中的文件,并确保并非所有值都是 NaN。只需在 MATLAB 中加载文件并检查内容:

load file.mat

这是我刚刚做的一个简单测试:

MATLAB

>> x = rand(3,4,2)

x(:,:,1) =
      0.75127      0.69908      0.54722      0.25751
       0.2551       0.8909      0.13862      0.84072
      0.50596      0.95929      0.14929      0.25428
x(:,:,2) =
      0.81428      0.34998      0.61604      0.83083
      0.24352       0.1966      0.47329      0.58526
      0.92926      0.25108      0.35166      0.54972

>> save file.mat x

R

R> library(R.matlab)
R> data <- readMat('file.mat')
R> str(data)
List of 1
 $ x: num [1:3, 1:4, 1:2] 0.751 0.255 0.506 0.699 0.891 ...
 - attr(*, "header")=List of 3
  ..$ description: chr "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: ..."
  ..$ version    : chr "5"
  ..$ endian     : chr "little"
R> data$x
, , 1

       [,1]   [,2]   [,3]   [,4]
[1,] 0.7513 0.6991 0.5472 0.2575
[2,] 0.2551 0.8909 0.1386 0.8407
[3,] 0.5060 0.9593 0.1493 0.2543

, , 2

       [,1]   [,2]   [,3]   [,4]
[1,] 0.8143 0.3500 0.6160 0.8308
[2,] 0.2435 0.1966 0.4733 0.5853
[3,] 0.9293 0.2511 0.3517 0.5497

关于r - 将数组从 matlab 导入 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11671883/

相关文章:

r - R中故障率和日期时间操作的计算

r - 根据格式为%d/%m/%Y的日期按时间顺序订购数据框

matlab - MATLAB变量传递和延迟分配

matlab - 检查大型矩阵 Matlab 中的重复条目

c# - 文件 IO streamreader + streamwriter C# 的问题

r - 带有 ggplot2 的箱线图 : Trying to lay geom_jitter over code for plot, 奇怪的离群点

r - 如何使用 ggplot 函数制作连续平滑的中位数?

string - 创建字符串元胞数组 matlab

python - 使用上下文管理器将脚本输出重定向到文件?

java - 从文件而不是从控制台读取