How can I change zeros matrix to 1 by specific points??

9 visualizaciones (últimos 30 días)
Mohammed Alammar
Mohammed Alammar el 27 de Nov. de 2019
Comentada: Mohammed Alammar el 28 de Nov. de 2019
I have 8X8 Zeros matrix :
A = zeros(8, 8);
I need to create a multiple rectangles of Ones matrix. when I have specific points location:
first rectangular startfrom point [1,1] which are (X0, Y0) to [3,3] which are (X1, Y2).
and
second rectangular start from point [5,5] which are (X0, Y0) to [8,8] which are (X1, Y2).
I have try this but does not work with me:
%%first rectangular
A(([1,1]:[3,1]):([3,1]:[3,3]))= 1
%%second rectangular
A(([5,5]:[8,5]):([8,5]:[8,8]))= 1
Could you please help me.

Respuesta aceptada

Adam Danz
Adam Danz el 27 de Nov. de 2019
Editada: Adam Danz el 27 de Nov. de 2019
Here's a demo. When [x0,y0] is [4,3] and [x1,y1] is [6,7], a rectangle of 1 will be placed in matrix A in rows 3:7 from the top and columns 4:6 from the left.
See inline comments for details.
% Create 0s matrix
A = zeros(8, 8);
% Define [x0,y0] and [x1,y1]
x0 = 4;
y0 = 3;
x1 = 6; % x1 >= x0
y1 = 7; % y1 >= y0
% replace section with 1s
[y,x] = meshgrid(x0:x1, y0:y1);
idx = sub2ind(size(A),x,y);
A(idx) = 1
Result
A =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0
0 0 0 1 1 1 0 0
0 0 0 1 1 1 0 0
0 0 0 1 1 1 0 0
0 0 0 1 1 1 0 0
0 0 0 0 0 0 0 0
If you'd rather use logicals, replace zeros() with false() and replace A(idx) = 1 with A(idx) = true.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 27 de Nov. de 2019
A = zeros(8, 8);
A(1:3,1:3) = 1;
A(5:8,5:8) = 1;
  1 comentario
Mohammed Alammar
Mohammed Alammar el 28 de Nov. de 2019
thank you i have a spicific point which is etract from other code.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by