Effacer les filtres
Effacer les filtres

Make elements of matrix ones

1 vue (au cours des 30 derniers jours)
LM
LM le 1 Juil 2021
Réponse apportée : Chunru le 1 Juil 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 commentaire
KSSV
KSSV le 1 Juil 2021
Terminate all the lines with semi colon, ;.

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 1 Juil 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')
  1 commentaire
LM
LM le 1 Juil 2021
Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (1)

Chunru
Chunru le 1 Juil 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)

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by