如何在matlab中处理立方体(3d数组)的所有面?

时间:2015-11-09 21:58:26

标签: matlab 3d indices

我需要将边界值分配给3d框。

假设我有z = rand(L,M,N),是否有更好的方法来处理此框的所有面,而不是单独处理所有6个面,z(:,:,1)z(:,:,end)z(:,1,:)z(:,end,:)z(1,:,:)z(end,:,:)

这就是我现在所拥有的:

L=3;M=4;N=5;
z = rand(L,M,N);
bv = 0;
z([1,end],:,:) = bv;
z(:,[1,end],:) = bv;
z(:,:,[1,end]) = bv;

我希望能够做z(indices) = bv之类的事情。

2 个答案:

答案 0 :(得分:1)

不确定这是否比您的代码更好,但在这里:

%// Data
L = 3;
M = 4;
N = 5;
z = rand(L,M,N)
newValue = 0;

%// Let's go
indL = false(L, 1, 1);
indM = false(1, M, 1);
indN = false(1, 1, N);
indL([1 end]) = true;
indM([1 end]) = true;
indN([1 end]) = true;
ind = bsxfun(@or, bsxfun(@or, indL, indM), indN); %// linear index of all faces
z(ind) = newValue

在:

z(:,:,1) =
    0.2653    0.7302    0.1078    0.8178
    0.8244    0.3439    0.9063    0.2607
    0.9827    0.5841    0.8797    0.5944
z(:,:,2) =
    0.0225    0.1615    0.0942    0.6959
    0.4253    0.1788    0.5985    0.6999
    0.3127    0.4229    0.4709    0.6385
z(:,:,3) =
    0.0336    0.5309    0.8200    0.5313
    0.0688    0.6544    0.7184    0.3251
    0.3196    0.4076    0.9686    0.1056
z(:,:,4) =
    0.6110    0.0908    0.2810    0.4574
    0.7788    0.2665    0.4401    0.8754
    0.4235    0.1537    0.5271    0.5181
z(:,:,5) =
    0.9436    0.2407    0.6718    0.2548
    0.6377    0.6761    0.6951    0.2240
    0.9577    0.2891    0.0680    0.6678

后:

z(:,:,1) =
     0     0     0     0
     0     0     0     0
     0     0     0     0
z(:,:,2) =
         0         0         0         0
         0    0.1788    0.5985         0
         0         0         0         0
z(:,:,3) =
         0         0         0         0
         0    0.6544    0.7184         0
         0         0         0         0
z(:,:,4) =
         0         0         0         0
         0    0.2665    0.4401         0
         0         0         0         0
z(:,:,5) =
     0     0     0     0
     0     0     0     0
     0     0     0     0

答案 1 :(得分:1)

如果您有图像处理工具箱,使用padarray将起作用:

z = padarray(z(2:end-1,2:end-1,2:end-1), [1 1 1], bv);

这只需要立方体的内部块,并在所有边上添加1份bv