matlab - 矩阵的值在其边缘周围平滑地下降到零

标签 matlab matrix memory multidimensional-array indexing

您好,我正在寻找一些帮助来优化一些 Matlab 代码。我有一个输入矩阵 M,它是 Ny 行乘以 Nx 列。我想围绕矩阵的边缘一直定义一个边界,其中的值应该平滑地衰减到零。为此,我尝试定义一个掩码函数 MSK 并将其逐点相乘以获得输出 N,如下所示:

enter image description here

enter image description here

我上面的尝试是使用下面的代码创建的,我在其中使用余弦平方函数来实现从 1 到零的平滑下降:

%%% Set up some grids %%%
Nx = 128;
Ny = 64;
dx = 0.1;
dy = 0.1;
x  = -(Nx*dx/2):dx:(Nx*dx/2-dx); 
y  = -(Ny*dy/2):dy:(Ny*dy/2-dy); 
x = permute(x, [1 2 3]); % Creates a [1 x Nx x 1] vector (saves memory)
y = permute(y, [2 1 3]); % Creates a [Ny x 1 x 1] vector

M = complex( double( rand(Ny,Nx)+10 ) ); % Create some input data

bnd_frac = 0.7; % The fraction of the grid to begin masking boundary, i.e 70% of the grid max

MSK = create_mask_function(x,y,Nx,Ny,bnd_frac);   % Create masking function

N = M.*MSK;  % Apply mask to input data, to smoothly ramp edges to zero

figure;
subplot(1,3,1);imagesc(x,y, abs(M) )
subplot(1,3,2);imagesc(x,y, abs(MSK) )
subplot(1,3,3);imagesc(x,y, abs(N) )

%%%%%%%%%%%%%%%%%%%%%%%%
%%% Masking Function %%%
%%%%%%%%%%%%%%%%%%%%%%%%
function MSK = create_mask_function(x,y,Nx,Ny,bnd_frac)

[~,bndind_x] = min( abs(x - bnd_frac*x(Nx)) ); % Find the index to begin the masking
x_bound = x(bndind_x);                         % Find the grid coordinate to begin masking
[~,bndind_y] = min( abs(y - bnd_frac*y(Ny)) );
y_bound = y(bndind_y);

dbnd_xp = x(Nx) - x_bound; % Find the width of the boundary region at each edge
dbnd_xm = x(1)  + x_bound;
dbnd_yp = y(Ny) - y_bound;
dbnd_ym = y(1)  + y_bound;

MSK = ones(Ny,Nx); % Initialise the mask matrix as ones

% Set the values within each boundary region at the edge to ramp smoothly to 0 from 1
MSK( : , x >  x_bound ) = repmat( ( cos( ( x(x>+x_bound) - x_bound )/dbnd_xp * pi/2 ) ).^2 , Ny , 1);
MSK( : , x < -x_bound ) = repmat( ( cos( ( x(x<-x_bound) + x_bound )/dbnd_xm * pi/2 ) ).^2 , Ny , 1);
MSK( y >  y_bound , : ) = repmat( ( cos( ( y(y>+y_bound) - y_bound )/dbnd_yp * pi/2 ) ).^2 , 1 , Nx);
MSK( y < -y_bound , : ) = repmat( ( cos( ( y(y<-y_bound) + y_bound )/dbnd_ym * pi/2 ) ).^2 , 1 , Nx);

MSK(:,Nx,:) = 0;
MSK(:,1,:)  = 0;
MSK(Ny,:,:) = 0;
MSK(1,:,:)  = 0;

end

上面代码的问题在于它为 MSK 分配了太多内存。

(在我的代码的真实版本中,所有矩阵都是 3D 的,实际上尺寸可能更接近 1024x512x512,但我在这里做了一个简化的 2D 示例来帮助解释。)

请注意,这就是我在上面的 xy 向量上使用 permute() 的原因,因为我的其他部分完整代码使用 Matlab 的 implicit expansion .这是为了避免存储使用 meshgrid() 生成的 x 和 y 的 3D 版本。

在上图中很明显,MSK 主要包含 1 值,并且渐变值仅出现在边缘附近。所以这似乎是存储那些 1 的大量内存浪费,因为它们的目的只是确保输入矩阵没有在中心被修改。任何人都可以帮助我理解在 Matlab 中实现我想要的东西但使用更少 RAM 的方法吗?

最佳答案

如果无法存储,则需要每次都重新计算权重并即时使用它们。

那么简短的回答是,而不是对 MSK 进行一些预计算,然后再使用,如:

MSK( : , x >  x_bound ) = repmat( ( cos( ( x(x>+x_bound) - x_bound )/dbnd_xp * pi/2 ) ).^2 , Ny , 1);
etc()
...
 
N = M.*MSK

你只想,在飞行中:

N( : , x >  x_bound ) = M( : , x >  x_bound ).*repmat( ( cos( ( x(x>+x_bound) - x_bound )/dbnd_xp * pi/2 ) ).^2 , Ny , 1);
etc()

我会把如何重组代码留给你,但我基本上只是将矩阵 M 作为输入添加到你的函数中,并将其重命名为“apply_mask”或类似名称。如果需要,可能会有进一步优化的方法,但“早期优化是万恶之源”。

关于matlab - 矩阵的值在其边缘周围平滑地下降到零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73866247/

相关文章:

Matlab 以编程方式将 native 库添加到路径

Matlab 可变精度

c - 如何在 C 中对二维数组使用 double for 循环?

在 C 中创建矩阵

memory - CUDA - 合并内存访问和总线宽度

C++ 新运算符

c - 我的简单游戏中的内存分配

matlab - Simulink:获取层次结构中较高模块的路径

matlab - Libsvm 分类 MATLAB

c++ - 从 sql 2008 表中获取 vector