Matlab 如何使用for循环将包含60个4乘4矩阵的mat文件分解为60个具有不同名称的不同矩阵?
我有一个mat文件(AA),其中有60个矩阵。我想将矩阵存储到单个矩阵中,并将它们以不同的名称保存在for循环中。但我得到的错误,我不知道如何摆脱。你有什么想法吗Matlab 如何使用for循环将包含60个4乘4矩阵的mat文件分解为60个具有不同名称的不同矩阵?,matlab,for-loop,matrix,Matlab,For Loop,Matrix,我有一个mat文件(AA),其中有60个矩阵。我想将矩阵存储到单个矩阵中,并将它们以不同的名称保存在for循环中。但我得到的错误,我不知道如何摆脱。你有什么想法吗 AA = randn(4,4,60); % this is the matrix w/ 60 matrices in it for i = 1:60 BB = zeros[4,4, length(AA)]; % pre-allocation BB(:,:,i) = AA(:,:,i); filename = ['BB-' num2
AA = randn(4,4,60); % this is the matrix w/ 60 matrices in it
for i = 1:60
BB = zeros[4,4, length(AA)]; % pre-allocation
BB(:,:,i) = AA(:,:,i);
filename = ['BB-' num2str(i), 'mat']; % rename the file to have different matrices
save(filename, 'BB')
end
但是它没有按照我想要的方式工作 以下是如何做到这一点:
AA = randn(4,4,60); % this is the matrix w/ 60 matrices in it
for i = 1:60
BB = AA(:,:,i);
filename = ['BB-' num2str(i), 'mat']; % rename the file to have different matrices
save(filename, 'BB')
end
无需预先分配
BB
以下是您的操作方法:
AA = randn(4,4,60); % this is the matrix w/ 60 matrices in it
for i = 1:60
BB = AA(:,:,i);
filename = ['BB-' num2str(i), 'mat']; % rename the file to have different matrices
save(filename, 'BB')
end
不需要预先分配
BB
预先分配应该在for
循环之前预先分配。预先分配应该在for
循环之前预先分配。你忘了中的点。哦,嗯,你也忘了p@TasosPapastylianou我不确定OP想要给他的文件起什么名字,所以我决定保持原样,让它工作起来。哈哈,是的。我只是觉得我应该先回答“为什么我的文件名错了”的问题:你忘了.mat
中的点哦,嗯,你也忘了p@TasosPapastylianou我不确定OP想要给他的文件起什么名字,所以我决定保持原样,让它工作起来。哈哈,是的。我只是觉得我应该抢先回答“为什么我的文件名错了”这个问题:p