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

6 Ansichten (letzte 30 Tage)
Mohammed Alammar
Mohammed Alammar am 27 Nov. 2019
Kommentiert: Mohammed Alammar am 28 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.

Akzeptierte Antwort

Adam Danz
Adam Danz am 27 Nov. 2019
Bearbeitet: Adam Danz am 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.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 27 Nov. 2019
A = zeros(8, 8);
A(1:3,1:3) = 1;
A(5:8,5:8) = 1;

Kategorien

Mehr zu Line Plots 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