Filter löschen
Filter löschen

Make elements of matrix ones

1 Ansicht (letzte 30 Tage)
LM
LM am 1 Jul. 2021
Beantwortet: Chunru am 1 Jul. 2021
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 Kommentar
KSSV
KSSV am 1 Jul. 2021
Terminate all the lines with semi colon, ;.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KSSV
KSSV am 1 Jul. 2021
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')

Weitere Antworten (1)

Chunru
Chunru am 1 Jul. 2021
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)

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by