我需要从实验室工作中处理大量数据。我有大量的.mat文件包含尺寸为7 x w的信号矩阵。我需要将矩阵的大小调整为7 x N并且w大于且小于N以使分析的其余部分更容易(不关心过去N的数据)。我有我想要这个如何工作的伪代码,但不知道如何实现它。任何帮助都会非常感谢!
我所有数据的文件夹结构:
主文件夹
Alpha 1
1111.mat
1321.mat
Alpha 2
1010.mat
1234.mat
1109.mat
933.mat
Alpha 3
1223.mat
等
Psudeocode:
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...
答案 0 :(得分:2)
首先,您需要生成文件列表。我有自己的功能,但有例如GETFILELIST或优秀的交互式UIPICKFILES来生成文件列表。
获得文件列表后(我假设它是包含文件名的单元格数组),您可以执行以下操作:
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
转换为7-by-by-nFiles数组,其中Master_matrix
的第n个平面是第n个文件的内容。在这种情况下,您将Master_matrix
初始化为
Master_matrix = zeros(7,N,nFiles);
并且您将if子句写为
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
,以便零不会影响后续统计信息(如果这是您要对其进行的操作数据)。