繁体   English   中英

如何生成具有功能模式的矩阵?

[英]How to generate a matrix with a function pattern?

背景信息 (可选阅读):

我正在模拟声波在边界上的反射。 使用矩阵设置空间点的介质条件。 假设空间的尺寸是N × N网格,并且我关心的是两种音速, c0c1

现在,我正在使用类似以下的代码来生成屏障模式

medium.sound_speed = c0*ones(N,N);   % set the speed of sound to be c0 everywhere
medium.sound_speed(:, N/2:N) = c1;   % set the right half of the grid to a different speed
medium.sound_speed(50:70, 50:70) = c1; % set a box to have a different speed

要么

% set all speeds to c0 except set the diagonal to c1
medium.sound_speed = c0*ones(N,N)-(c0*eye(N,N))+c1*eye(N,N); 

但是,我无法生成具有不同曲率的更复杂的边界。

我想以编程方式创建具有反映功能的模式的矩阵。 例如,我想输入f(x)=2并为此创建一个看起来像这样的矩阵,假设N=6

[ 0 0 0 0 0 0
  0 0 0 0 0 0
  0 0 0 0 0 0 
  1 1 1 1 1 1 
  0 0 0 0 0 0 
  0 0 0 0 0 0 ]

f(x)=0.5*x+1

[ 0 0 0 0 0 0
  0 0 0 0 0 0
  0 0 0 0 1 1
  0 0 1 1 0 0 
  1 1 0 0 0 0 
  0 0 0 0 0 0]

我还能够生成类似f(x)=1/x弯曲图案,这似乎需要某种形式的Midpoint圆算法 ,用于绘制带有像素的曲率。

[ 1 0 0 0 0 0
  1 0 0 0 0 0
  0 1 0 0 0 0
  0 0 1 1 0 0
  0 0 0 0 1 1  
  0 0 0 0 0 0 ]

实际上, N至少为128,因此为具有某种程度复杂性的形状手动创建这些矩阵是不切实际的,我认为这是一个有趣的问题。

有人知道这样做的方法吗,或者对替代方法的建议?

先感谢您。

编辑:我修改这个实现布氏算法,以提供给定的起点和终点所需的行的矩阵。

function M=bresenham_line(point)

if (abs(point(4)-point(2)) > abs(point(3)-point(1)))       % If the line is steep                                
    x0 = point(2);y0 = point(1); x1 = point(4);y1=point(3);% then it would be converted to 
    token =1;                                              % non steep by changing coordinate
else
    x0 = point(1);y0 = point(2); x1 = point(3);y1=point(4);
    token = 0; 
end
if(x0 >x1)
    temp1 = x0; x0 = x1; x1 = temp1;
    temp2 = y0; y0 = y1; y1 = temp2;
end
dx = abs(x1 - x0) ;                              % Distance to travel in x-direction
dy = abs(y1 - y0);                               % Distance to travel in y-direction
sx = sign(x1 - x0);                              % sx indicates direction of travel in X-dir
sy = sign(y1 - y0);                              % Ensures positive slope line

x = x0; y = y0;                                  % Initialization of line
param = 2*dy - dx ;                              % Initialization of error parameter
for i = 0:dx-1                                   % FOR loop to travel along X
    x_coord(i+1) = x;                            % Saving in matrix form for plot
    y_coord(i+1) = y;

    param = param + 2*dy;                        % parameter value is modified
    if (param >0)                                % if parameter value is exceeded
        y = y +1*sy;                             % then y coordinate is increased
        param = param - 2*(dx );                 % and parameter value is decreased

    end
    x = x + 1*sx;                                % X-coordinate is increased for next point
end

M = zeros(size(x_coord,2), size(y_coord,2));

for i=1:1:size(x_coord,2)
    x = x_coord(i);
    y = y_coord(i);

    M(x,y) = 1;
end

M

像这样实现:

c1 = 0;
M = bresenham_line([1 1 Nx/2+1 Ny+1]);
medium.sound_speed = c0*ones(Nx,Ny) - (c0*M) + c1*M; 

弯曲函数形状尚无进展。

尽管这样最好,但我最好打赌布雷森汉姆的算法可能会有点“肮脏”。

N = 128;
[X,Y] = meshgrid(1:N,1:N);
bound1 = Y<2*X;
bound2 = Y<2*X+1;
M = xor(bound1,bound2);

bound1可以定义任何函数y=f(x) ,并标记其下的区域。 使用bound2您选择的区域会稍高一些(向上移动)。 一旦你把和xor两个区域的,你得到的只是所需的y=f(x)标记。 我认为,为了获得合理的结果,对于更复杂的功能而言,移位可能有所不同。

为了说明起见,我使用了imagescflipud仅用于使(0,0)位于左下角,而不是左上角):

imagesc(flipud(M));

编辑

确实,对于某些功能,这可能不是最好的。 例如,对于y=x^2 ,您必须增加偏移量,但看起来仍然不太好。

bound1 = Y<X.^2;
bound2 = Y<X.^2+15;
M = xor(bound1,bound2);

一种获得类似结果的方法:

f = @(x)0.5*x; %create the function (x should be written even if the function doesn't depend on x: @(x) 0*x + 2)
N = 6; %choose the size of the atrix
M = zeros(N,N); %create an empty matrix
x = (1:N); 
y = round(f(x-1)); %discretization
x(y>N-1|y<0) = [];
y(y>N-1|y<0) = [];
M(sub2ind(size(M),y+1,x)) = 1;
M = flipud(M)

因此,您可以选择函数,然后矩阵中的结果将看起来像是正态plot的离散化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM