Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何使用for循环将包含60个4乘4矩阵的mat文件分解为60个具有不同名称的不同矩阵?_Matlab_For Loop_Matrix - Fatal编程技术网

Matlab 如何使用for循环将包含60个4乘4矩阵的mat文件分解为60个具有不同名称的不同矩阵?

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

我有一个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-' 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