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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by