Make elements of matrix ones

1 次查看(过去 30 天)
LM
LM 2021-7-1
回答: Chunru 2021-7-1
I created a zero matrix with rectangular elements of 255. My script below is working, but I don't think this is an efficient way to do this. I want to generate 3 bars of varying pixel widths in the north, south, east, west and center of a 248x286 matrix. I would appreciate input on how to optimize the script. Thank you.
A = zeros(248,286); %create matrix of zeros
A(20:60,124:134) = 1 %north: rectangles 40 pixels long and 10 pixels wide
A(20:60,144:154) = 1
A(20:60,164:174) = 1
A(70:120,124:130) = 1 %north: rectangles 40 pixels long and 6 pixels wide
A(70:120,140:146) = 1
A(70:120,156:160) = 1
A(130:170,124:126) = 1 %north: rectangles 40 pixels long and 2 pixels wide
A(130:170,136:138) = 1
A(130:170,148:150) = 1
m = A*255;
imshow(m)
imwrite(m, 'Verticle_bars.bmp')

采纳的回答

KSSV
KSSV 2021-7-1
A = zeros(248,286); %create matrix of zeros
A(20:60,[124:134 144:154 164:174]) = 1 ; %north: rectangles 40 pixels long and 10 pixels wide
A(70:120,[124:130 140:146 156:160] ) = 1 ;%north: rectangles 40 pixels long and 6 pixels wide
A(130:170,[124:126 136:138 148:150] ) = 1 ;%north: rectangles 40 pixels long and 2 pixels wide
m = A*255;
imshow(m)
% imwrite(m, 'Verticle_bars.bmp')

更多回答(1 个)

Chunru
Chunru 2021-7-1
A slightly simpler version:
A = zeros(248,286); %create matrix of zeros
A(20:60,[124:134 144:154 164:174]) = 1;
A(70:120, [124:130 140:146 156:160]) = 1;
A(130:170, [124:126 136:138 148:150]) = 1;
m = A*255;
imshow(m)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by