Creating a cross pattern in a 128 x 128 matrix in MATLAB

I am trying get get a head start on courses and I've run into a sample problem in my textbook about creating a 128 x 128 of zeros and then filling in a cross pattern of ones in the center of this matrix. I am trying to minimize use of forloops
I was thinking of concatenating matrices, but I am a MATLAB newbie it would also be annoying to designate which positions need to be filled and what not if I concatenate
I am wondering if there is a rectangle function that I can use to create a horizontal and vertical rectangle to make the cross pattern or any other suggestions
Also, is there a way to zoom out on the output....as it is very hard to check a 128 x 128 matrix??? (since the output is broken up into rows and columns)
Thank you very much

 Accepted Answer

Do you mean like this:
workspace;
m = zeros(100)
row1 = 45;
row2 = 54;
m(row1:row2,:) = 255;
m(:,row1:row2) = 255;
image(m);
You can use the variable inspector to look at the values, just double click on the variable name in the workspace panel.

4 Comments

I figured it out--the cross pattern is supposed to be centered and is not supposed to touch the sides I just used a rectangle tool to create two perpendicular rectangles However, as a noob, I really appreciate your method, I think I can find how to manipulate it to get my desired image.
Thank you!
You can go from 2 to (end-1). Here is how I'd do it:
m = 100*ones(10, 10, 'uint8')
row1 = 4;
row2 = 7;
m(row1:row2,2:end-1) = 255;
m(2:end-1,row1:row2) = 255;
image(m);
colormap(gray(256));
grid on;
Guys i need your help!!!! I have question Using a built-in function to create a 30 x 30 matrix, where the middle two róws and the middle two columns are filled with 2's while the rest are 4's.
m = 4 * ones(30, 30);
m(15:16, :) = 2;
m(:, 15:16) = 2;

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!