【问题标题】:Read and reshape many matrices from many large .mat files efficiently有效地从许多大型 .mat 文件中读取和重塑许多矩阵
【发布时间】:2013-12-17 07:50:43
【问题描述】:

我有将近一千个名称奇怪的 .mat 文件:

我的第一个问题是运行一个 foo 循环来打开这样命名的文件:

exp_trial1_0001
exp_trial1_0002
...
exp_trial1_1000

它是 4 位数的事实让我很难。这行不通:

load(['exp_trial_', num2str(%04i), '.mat'])

有 1000 个 .mat 文件对应于 1000 次试验。在每个 .mat 文件中都有一个矩阵 A。矩阵 A 有 500 行。我想取出每个 .mat 文件(“试用”)的所有第 1 行(和 2s、3s、... 500)并将它们放在一个单独的矩阵中。

我无法同时加载所有这些 .mat 文件,然后再执行此操作,因为内存不足。我想知道最有效的方法是什么。

非常感谢!

【问题讨论】:

    标签: matlab


    【解决方案1】:

    我建议以下几点:

    N = 100; %// Number of columns of the matrix "A" in the files. Set as needed. 
    F = 1000; %// Number of files
    rows = [1 2 3]; %// rows you want to extract from the files
    result = NaN(numel(rows),N,F); %// preallocate
    for ii = 1:F %// iterate over all files
        load(['exp_trial1_' sprintf('%04d',ii) '.mat'], 'A'); %// load matrix A only
        result(:,:,ii) = A(rows,:); %// store desired rows from each file
    end
    

    【讨论】:

    • 这似乎不起作用。 LOAD 错误(第 8 行) result(:,:,i) = A(rows,:);此外,我想将每一行保存为 .mat 文件,而不是将它们放在另一个巨大的矩阵中。非常感谢!
    • @user3108418 要保存在不同的文件中,请在变量rows 中只选择一行,将result 保存在 .mat 文件中,然后对每个所需的行重复
    猜你喜欢
    • 2014-06-19
    • 2019-09-06
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多