How do I make a rectangular mask of 1's?

Hi. I have a matrix of 768x1024. Now, within this matrix, I want to have a rectangle such that it is centered around the middle column of the matrix and has dimension (k)x1024, where k is a variable. This (k)x1024 rectangular portion within the 768x1024 matrix should have all entries 1 and the rest of the matrix entries should all be zero. So, it creates sort of a mask so that when I multiply it with another 768x1024 matrix, the result is only the rectangular part of the other matrix. All help is appreciated. Thanks.

 Accepted Answer

Assuming you mean centered around the middle ROW:
% Initialize to all zeros.
mask = zeros(768, 1024);
topRow = floor(768/2 - k/2); % Find the top row.
% Assign the 1's.
mask(topRow:(topRow+k-1),:) = 1;
Since the 1's go all the way across all 1024 columns, you don't really have centering or not centered when you're talking about the columns.

5 Comments

Wait a minute. Do you want a mask k rows by 1024 columns, or is k a factor and you want (k times 1024) rows, by 1024 columns? Or do you not even want 1024 columns? Please clarify.
I want the 1's rectangle to have dimensions of k rows and 1024 columns within a rectangle of 768 rows and 1024 columns where the 1's rectangle is centered around the middle column of the bigger matrix and the rest of the bigger matrix entries are zero.
OK, see edited code above.
Thanks man. This worked. Cheers!

Sign in to comment.

More Answers (0)

Tags

Asked:

Ali
on 7 Mar 2014

Commented:

Ali
on 8 Mar 2014

Community Treasure Hunt

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

Start Hunting!