在Octave中将模式应用于矩阵

时间:2018-04-05 15:45:10

标签: matrix 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]

如何才能实现这一目标?

谢谢!

1 个答案:

答案 0 :(得分:2)

两种解决方案:

使用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

使用图片包中的im2colcol2im

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