首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动加载多个*.mat文件和调整矩阵大小

问自动加载多个*.mat文件和调整矩阵大小
EN

Stack Overflow用户
提问于 2010-11-12 01:38:58
回答 1查看 1.9K关注 0票数 2

我有一大堆实验室工作的数据需要处理。我有一大堆.mat文件,其中包含一个尺寸为7 x w的信号矩阵。我需要将矩阵大小调整为7 x N,w比N大也比N小,以使其余的分析更容易(不关心超过N的数据)。我有我想要如何工作的伪代码,但不知道如何实现它。任何帮助都将是非常感谢的!

我所有数据的文件夹结构:

主文件夹

代码语言:javascript
复制
Alpha 1
    1111.mat
    1321.mat
Alpha 2
    1010.mat
    1234.mat
    1109.mat
    933.mat
Alpha 3
    1223.mat

等。

伪码:

代码语言:javascript
复制
    Master_matrix = []
    For all n *.mat
        Load n'th *.mat from alpha 1
        If w > N
            Resize matrix down to N
        Else
            Zero pad to N
        End if
    Master_matrix = master_matrix .+ new resized matrix
    End for

rest of my code...
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-12 02:24:03

首先,您需要生成文件列表。我有我自己的函数,但是,例如,有GETFILELIST或优秀的交互式UIPICKFILES来生成文件列表。

有了文件列表(我假设它是一个包含文件名的单元格数组)之后,您可以执行以下操作:

代码语言:javascript
复制
nFiles = length(fileList);
Master_matrix = zeros(7,N);

for iFile = 1:nFiles
    %# if all files contain a variable of the same name, 
    %# you can simplify the loading by not assigning an output
    %# in the load command, and call the file by
    %# its variable name (i.e. replace 'loadedData')
    tmp = load(fileList{iFile});
    fn = fieldnames(tmp);
    loadedData = tmp.(fn{1});

    %# find size 
    w = size(loadedData,2);

    if w>=N
       Master_matrix = Master_matrix + loadedData(:,1:N);
    else
       %# only adding to the first few columns is the same as zero-padding
       Master_matrix(:,1:w) = Master_matrix(:,1:w) = loadedData;
    end
end

注意:如果您实际上不想将数据相加,而只是将其存储在主数组中,则可以将Master_matrix转换为一个7xNxnFiles数组,其中Master_matrix的第n个平面是第n个文件的内容。在本例中,您可以将Master_matrix初始化为

代码语言:javascript
复制
Master_matrix = zeros(7,N,nFiles);

你可以把if子句写成

代码语言:javascript
复制
    if w>=N
       Master_matrix(:,:,iFile) = Master_matrix(:,:,iFile) + loadedData(:,1:N);
    else
       %# only adding to the first few columns is the same as zero-padding
       Master_matrix(:,1:w,iFile) = Master_matrix(:,1:w,iFile) = loadedData;
    end

还要注意,您可能希望将Master_matrix初始化为NaN而不是zeros,这样零就不会影响后续的统计信息(如果您想要对数据执行此操作)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4157419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档