Convolution Mask

조회 수: 5 (최근 30일)
Joseph
Joseph 2011년 9월 19일
To start, I have 3 matrices:
m1= [3 x 3]
m2= [5 x 5]
m2= [7 x7]
I have three loops that are used depending on the size of the matrix. I want to use an "if" command to decifer the size of matrix so the appropriate loop is used.
Here is an example of the 3 x 3 case.
function [matrix]= three_by_three(x,y,I)
if m1 =
matrix= [I(x-1, y-1), I(x-1, y), I(x-1, y+1); I(x, y-1), I(x,y), I(x, y+1); I(x+1, y-1), I(x+1, y), I(x+1, y+1)];
end

채택된 답변

David Young
David Young 2011년 9월 19일
Something like this?
sz = size(matrix);
if isequal(sz, [3 3])
% first for loop, function call or whatever
elseif isequal(sz, [5 5])
% second
elseif isequal(sz, [7 7])
% third
else
error('myfunction:badsize', 'Unexpected matrix size');
end
You could also use a switch statement.

추가 답변 (1개)

Image Analyst
Image Analyst 2011년 9월 28일
No need to have multiple functions to do this. A single line of code will extract any size submatrix you want. It all comes down to a basic MATLAB instruction like
subMatrix = fullSizeMatrix(row1:row2, col1:col2);
Here's a full demo:
% Generate sample data.
matrix = floor(6*rand(15))+1
[rows columns] = size(matrix)
% Define center of submatrix to extract.
row = 4;
column = 6
% First example: Extract a 3x3 chunk.
sizeToExtract = 3;
halfWidth = floor(sizeToExtract / 2)
subMatrix = matrix(row-halfWidth:row+halfWidth, column-halfWidth:column+halfWidth)
%Second example: Extract a 5x5 chunk.
sizeToExtract = 5;
halfWidth = floor(sizeToExtract / 2)
subMatrix = matrix(row-halfWidth:row+halfWidth, column-halfWidth:column+halfWidth)
By the way, you do know that there is a function called conv2() that does convolution so you don't need to reinvent it, don't you?
  댓글 수: 1
Joseph
Joseph 2011년 9월 28일
Yes, thanks for the response. However, we had to write our own for a class.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Digital Filtering에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by