将较小的3D矩阵放入较大的3D矩阵(3D sub2ind) [英] Putting a smaller 3D matrix into a bigger 3D matrix (3D sub2ind)

查看:102
本文介绍了将较小的3D矩阵放入较大的3D矩阵(3D sub2ind)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将较小的3D矩阵放入较大的3D矩阵中.举例说明:

I need to put a smaller 3D matrix into a bigger 3D matrix. Explaining with an example:

假设我具有以下3D矩阵:

Suppose I have the following 3D matrices:

%A is the big matrix
A(:,:,1)=[ 0.3545    0.8865    0.2177
           0.9713    0.4547    0.1257
           0.3464    0.4134    0.3089];

A(:,:,2)=[  0.7261    0.0098    0.7710
            0.7829    0.8432    0.0427
            0.6938    0.9223    0.3782];

A(:,:,3) = [0.7043    0.2691    0.6237
            0.7295    0.6730    0.2364
            0.2243    0.4775    0.1771];

%B is the small matrix
B(:,:,1) = [0.3909    0.5013 
            0.0546    0.4317];


B(:,:,2) =[0.4857    0.1375 
           0.8944    0.3900];

B(:,:,3) =[0.7136    0.3433
           0.6183    0.9360];

现在将B放入A中,使得:使用第一维度:[1 3],第二维度[2 3],然后对A的[1,2,3]页执行此操作.对于给定的矩阵,将这些值放入将导致:

Now to put B in A such that: use first dimension: [1 3], second dimension [2 3] and do this for [1,2,3] pages of A. For the given matrix, putting these values will result in:

NewA(:,:,1) = [ 0.3545    0.3909    0.5013     % putting the value of %B(1,:,1)
                0.9713    0.4547    0.1257
                0.3464    0.0546    0.4317;   % putting the value of %B(2,:,1)


NewA(:,:,2)=[  0.7261    0.4857    0.1375      % putting the value of %B(1,:,2)
               0.7829    0.8432    0.0427
               0.6938    0.8944    0.3900];    % putting the value of %B(2,:,2)


NewA(:,:,3) = [0.7043    0.7136    0.3433      % putting the value of %B(1,:,3)
               0.7295    0.6730    0.2364
               0.2243    0.6183    0.9360];    % putting the value of %B(2,:,3)

由于3D页面,我不一定具有正方形矩阵,放入BA的大小也可以变化.但是矩阵将始终是3D.以上只是一个小例子.我实际上拥有的尺寸和A-> [500,500,5]和B--[350,350,4]一样大.

I won't necessarily have square matrices as 3D pages and the size of A to put B in can vary as well. But the matrices will always be 3D. Above is just a small example. What I actually have is dimensions as big as A -> [500,500,5] and B as -> [350,350,4].

这是sub2ind用于2D矩阵的方法,但是我还不能操纵3D矩阵.

This is what sub2ind do for 2D matrices but I am not yet able to manipulate into use for 3D matrices.

类似的东西:

NewA = A;  
NewA(sub2ind(size(A), [1 3], [2 3], [1 2 3])) = B;

但是它给出了:

Error using sub2ind (line 69)
The subscript vectors must all be of the same size.

我该怎么做?

推荐答案

您不需要sub2ind,只需直接分配:

You don't need sub2ind, just assign directly:

newA(1,2:3,:)=B(1,:,:)


如果要使用sub2ind,则需要为要替换的每个元素指定3个维度中的每个维度:


If you want to use sub2ind, you need to specify each of the 3 dimensions, for each of the elements you want to replace:

dim1A=[1 1 1 1 1 1];  % always first row
dim2A=[2 3 2 3 2 3];  % second and third column, for each slice
dim3A=[1 1 2 2 3 3];  % two elements from each slice

newA(sub2ind(size(A),dim1A,dim2A,dim3A))=B(1,:,:)

newA(:,:,1) =

    0.3545    0.3909    0.5013
    0.9713    0.4547    0.1257
    0.3464    0.4134    0.3089


newA(:,:,2) =

    0.7261    0.4857    0.1375
    0.7829    0.8432    0.0427
    0.6938    0.9223    0.3782


newA(:,:,3) =

    0.7043    0.7136    0.3433
    0.7295    0.6730    0.2364
    0.2243    0.4775    0.1771

这篇关于将较小的3D矩阵放入较大的3D矩阵(3D sub2ind)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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