我需要把一个更小的3D矩阵放到一个更大的3D矩阵中。举例说明:
假设我有以下三维矩阵:
%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维:1: 3,第2维:3,然后对给定矩阵A的1、2、3页执行此操作,将这些值放入这些值将得到以下结果:
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页面那样有方阵,A的大小也可以改变B的大小。但是矩阵总是三维的。以上只是一个小小的例子。我的实际尺寸是A,-> 500,500,5,B,-> 350,350,4。
这是sub2ind为2D矩阵所做的,但我还不能将其操作到3D矩阵中。
类似于:
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.我该怎么做?
发布于 2017-10-19 09:49:10
您不需要sub2ind,只需直接分配:
newA(1,2:3,:)=B(1,:,:)如果要使用sub2ind,则需要为要替换的每个元素指定三个维度中的每一个:
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.1771https://stackoverflow.com/questions/46826642
复制相似问题