3-D对象的多维数组:如何对内积进行矢量化 [英] Multidimensional arrays of 3-D objects: how to vectorise inner products

查看:118
本文介绍了3-D对象的多维数组:如何对内积进行矢量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题中,我讨论了两个自定义函数以将3x3矩阵和3x1向量,保留了3维(矩阵)内积的结构,并使整个过程尽可能高效地进行计算.

In this question, I discussed two custom functions to multiply arrays of 3x3 matrices and 3x1 vectors, preserving the structure of the 3-dimensional (matrix) inner product and making the whole process as computationally efficient and fast as possible.

我现在将这些函数推广到3x4矩阵和3x1向量的多维数组(NxN).这是我编写的使用for循环的函数.

I have now generalised those functions to multidimensional arrays (NxN) of 3x4 matrices and 3x1 vectors. Here are the functions I have written, which make use of for loops.

BlockScalar

此函数应将(NxN矩阵)nv的ij元素(标量)乘以A(NxNx3x3矩阵)的ij元素(3x3矩阵).因此,它本质上是标量矩阵乘积的多维版本.

This function should multiply the ij element (a scalar) of the (NxN matrix) nv by the ij element (3x3 matrix) of A (NxNx3x3 matrix). So it's essentially a multidimensional version of the product of a matrix by a scalar.

function [B] = BlockScalar(nv,A)

    N=size(nv,1);
    B=zeros(N,N,3,3);

    for i=1:N
        for j=1:N 
            B(i,j,:,:)= nv(i,j).*A(i,j,:,:);
        end
    end

end

-------- BlockScalar示例

--------BlockScalar Example

输入:

N=2;
A = shiftdim( repmat( eye(3,3), 1, 1, N, N ), 2 );
nv=[1 2; 3 4];

输出:

BlockScalar(nv,A)

BlockScalar(nv,A)

ans(:,:,1,1)=

ans(:,:,1,1) =

 1     2
 3     4

ans(:,:,2,1)=

ans(:,:,2,1) =

 0     0
 0     0

ans(:,:,3,1)=

ans(:,:,3,1) =

 0     0
 0     0

ans(:,:,1,2)=

ans(:,:,1,2) =

 0     0
 0     0

ans(:,:,2,2)=

ans(:,:,2,2) =

 1     2
 3     4

ans(:,:,3,2)=

ans(:,:,3,2) =

 0     0
 0     0

ans(:,:,1,3)=

ans(:,:,1,3) =

 0     0
 0     0

ans(:,:,2,3)=

ans(:,:,2,3) =

 0     0
 0     0

ans(:,:,3,3)=

ans(:,:,3,3) =

 1     2
 3     4

BlockMatrix

此第二个函数目前无法正常工作,因为我正在努力实现A的第ij个元素(为3x3矩阵)与包含3个成分的列向量之间的矩阵乘积A*u u的第ij个元素的元素.如您所见,我希望这是3-D矩阵*矢量积的多维概括.

This second function does not work at the moment because I am struggling to implement the matrix product A*u between the ij-th element (which is a 3x3 matrix) of A and a column vector that contains the 3 components of the ijth element of u. As you may easily see, I would like this to be a multidimensional generalisation of the matrix*vector product in 3-D.

 function [B] = BlockMatrix(A,u)

    N = size(u,2);
    B = zeros(N,N,3);

  for i=1:N
     for j=1:N
             B(i,j,:)= reshape(reshape(A(i,j,:,:),[3,3])*reshape(u(i,j,:),[1 3]),size(u));
     end

 end

------- BlockMatrix示例

如果输入是一个广义的恒等矩阵(NxN个元素,每个元素是一个3x3恒等矩阵),以及一个由3x1个向量组成的NxN矩阵:

If the input is a generalised identity matrix (NxN elements each of which is a 3x3 identity matrix), and an NxN matrix made of 3x1 vectors:

 N=2;   

 A = 4.*shiftdim( repmat( eye(3,3), 1, 1, N, N ), 2 );

 c = ones(2,2);
 V(1,1,:)=[1 2 3];
 u = c.*V;

所需的输出显然是一个具有V结构(由3x1矢量组成的NxN矩阵)的对象,其中每个元素都是reshape(A(i,j,:,:),[3 3])reshape(V(i,j,:),[1 3])的矩阵乘积.那是:

The desired output is clearly an object which has the structure of V (NxN matrix made of 3x1 vectors) where each of the elements is the matrix product of reshape(A(i,j,:,:),[3 3]) and reshape(V(i,j,:),[1 3]). That is:

i=1;j=1;
reshape(B(i,j,:),[3,1])


ans =

 4
 8
 12

用于任何ij.

完整输出,以确保完整性:

Full output, for completeness:

B(:,:,1)=

B(:,:,1) =

 4     4
 4     4

B(:,:,2)=

B(:,:,2) =

 8     8
 8     8

B(:,:,3)=

B(:,:,3) =

 12     12
 12     12

问题

我努力(0)使BlockMatrix工作; (1)找到一种将其正确向量化的方法,以及(2)我什至还不确定向量化的版本会更快.

I struggle to (0) make BlockMatrix work; (1) find a way to properly vectorise this, and (2) I am not even particularly sure the vectorised version would be faster.

在回答以上问题时提供的任何帮助将不胜感激.

Any help in answering the points above will be much appreciated.

推荐答案

对于第一个功能:

B = bsxfun(@times, A, nv);

第二次:

B = sum(bsxfun(@times, A, reshape(u, [size(u,1) size(u,2) 1 size(u,3)])), 4);

这篇关于3-D对象的多维数组:如何对内积进行矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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