将模式应用于 Octave 中的矩阵

Applying a pattern to a matrix in Octave

我正在使用 Octave。我有一个随机的 MxN 矩阵。我想使用预定义模式将矩阵中的某些值设置为零:

Matrix:           Pattern:        Result:
[45 88 93 25 23     [1 0            [45  0 93  0 23 
 13 55  4 90  9      0 0]             0  0  0  0  0
 34 87 67 07 88                      34  0 67  0  9 
 13 57 14 16 27                       0  0  0  0  0
 94 35 68 22 89]                     94  0 68  0 89]

如何做到这一点?

谢谢!

两种解决方案:

使用repmat

ms = size(Matrix);
ps = size(Pattern); 
P = repmat(Pattern, ceil(ms ./ ps));    % repeat the pattern
result = Matrix .* resize(P , ms);      % resize and apply the pattern
%result = Matrix .* P(1:ms(1),1:ms(2)); % same as above

使用图像包中的im2col and col2im

pkg load image
ms = size(Matrix)
ps = size(Pattern)
col = im2col(Matrix,ps,'distinct');        % convert image to columns
colpat = col .* Pattern(:);                % apply the pattern 
result = col2im(colpat, ps,ms,'distinct'); % convert column back to image