Azzera filtri
Azzera filtri

Make elements of matrix ones

1 visualizzazione (ultimi 30 giorni)
LM
LM il 1 Lug 2021
Risposto: Chunru il 1 Lug 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 Commento
KSSV
KSSV il 1 Lug 2021
Terminate all the lines with semi colon, ;.

Accedi per commentare.

Risposta accettata

KSSV
KSSV il 1 Lug 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')

Più risposte (1)

Chunru
Chunru il 1 Lug 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)

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by