如何遍历Matlab中的多个矩阵 [英] How to traverse over multiple matrices in matlab
问题描述
我有一个Mat文件,加载后会给我这样的东西:
I've a mat file which when loaded gives me something like this:
train0:[1200x300] train1:[1450x300] . . . . . . trainN:[Nx300]
train0:[1200x300] train1:[1450x300] . . . . . . trainN:[Nx300]
我想要做的是像train +"i"那样遍历每个矩阵,其中i = 0到N 并创建一个值为i的NX1矩阵. N是每个训练矩阵中的行数.
what I want to do is traverse over each matrices in a manner like train+"i" where i = 0 to N and create a NX1 matrix with value of i. Here N will be the number of rows in each of the train matrices.
推荐答案
尝试将文件加载到sturct
try loading the file into a sturct
ld = load(matfilename);
numRow = structfun( @(x) size(x,1), ld );
更复杂的方法可能是:
ld = load(matfilename);
matNames = fieldnames( ld );
numRows = zeros( 1, numel(matNames) );
for fi = 1:nueml(matNames)
tkn = regexp( matNames{fi}, '^train(\d+)$', 'tokens', 'once' );
ii = str2double( tkn{1} );
numRows(ii) = size( ld.(matNames{fi}), 1 );
end
无论如何,将mat文件加载到结构中后,您就可以将所有已加载的矩阵作为结构字段进行操作.
Anyhow, loading the mat file into a struct allows you to manipulate all loaded matrices as struct fields.
这篇关于如何遍历Matlab中的多个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!