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

9 vues (au cours des 30 derniers jours)
Mohammed Alammar
Mohammed Alammar le 27 Nov 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.

Réponse acceptée

Adam Danz
Adam Danz le 27 Nov 2019
Modifié(e) : Adam Danz le 27 Nov 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.

Plus de réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices 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