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

조회 수: 9 (최근 30일)
Mohammed Alammar
Mohammed Alammar 2019년 11월 27일
댓글: Mohammed Alammar 2019년 11월 28일
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.

채택된 답변

Adam Danz
Adam Danz 2019년 11월 27일
편집: Adam Danz 2019년 11월 27일
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.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2019년 11월 27일
A = zeros(8, 8);
A(1:3,1:3) = 1;
A(5:8,5:8) = 1;
  댓글 수: 1
Mohammed Alammar
Mohammed Alammar 2019년 11월 28일
thank you i have a spicific point which is etract from other code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by