是否可以使用mldivide“\"?在 Matlab 中的 3D 矩阵上 [英] Is it possible to use mldivide "\" on a 3D matrix in Matlab

查看:35
本文介绍了是否可以使用mldivide“\"?在 Matlab 中的 3D 矩阵上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 MATLAB 的 3D 矩阵上使用 mldivide (\)?我想避免使用 for 循环?

Is it possible to use mldivide (\) on a 3D matrix in MATLAB? I would like to avoid using a for loop?

示例:

A = rand(4, 100, 5);
B = rand(4,4);

我需要执行:

C = B\A;

我现在在做什么:

在 for 循环上为每个切片"i 应用 mldivide:

Apply the mldivide on a for loop for each "slice" i:

for i = 1:size(A, 3)    
    C(:,:,i) = B \ A(:,:,i); 
end

推荐答案

您可以将 A 重塑为二维矩阵以执行除法,然后再恢复到预期大小.reshape 操作应该相对较快,因为 MATLAB 不会改变底层数据.

You can reshape A into a 2D matrix to perform the division and then back to the expected size afterwards. The reshape operations should be relatively quick due to the fact that MATLAB doesn't alter the underlying data.

C = reshape(B \ reshape(A, size(A, 1), []), size(B, 1), size(A, 2), []);

如果我们把它分解:

%// Reshape A to be 4 x 500
Anew = reshape(A, size(A, 1), []);

%// Perform left division
C = B \ Anew;

%// Reshape C to be the expected size (4 x 100 x 5)
C = reshape(C, size(B, 1), size(A, 2), []);

这应该适用于任何有效的 (size(A, 1) == size(B, 2)) 矩阵 AB任何大小.

This should work for any valid (size(A, 1) == size(B, 2)) matrices A and B of any size.

这篇关于是否可以使用mldivide“\"?在 Matlab 中的 3D 矩阵上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆