필터 지우기
필터 지우기

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')
  댓글 수: 1
KSSV
KSSV 2021년 7월 1일
Terminate all the lines with semi colon, ;.

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

채택된 답변

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
LM
LM 2021년 7월 1일
Thank you very much.

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

추가 답변 (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