Create Matrix using only zeros and ones

please how can i create this matrix using only the two commands "zeros" and "ones"
1 0 1 0
1 0 1 0
1 0 1 0
1 0 1 0
and this one :
0 0 0 1
0 0 0 1
0 0 0 1
1 1 1 1

3 件のコメント

Steven Lord
Steven Lord 2020 年 5 月 16 日
Assuming that you're actually allowed to use zeros, ones, and concatenation, since this sounds like homework I won't give the solution. But I will give a hint: break the matrices into rectangular pieces where each piece consists only of 0's or 1's. Build the pieces then put the pieces together.
lamiae hmimou
lamiae hmimou 2020 年 5 月 17 日
i found out how to do it ! thanks for the hint it's a good method
M VENKATESH
M VENKATESH 2021 年 8 月 31 日
How

サインインしてコメントする。

回答 (2 件)

William Alberg
William Alberg 2020 年 5 月 16 日
編集済み: William Alberg 2020 年 5 月 16 日

0 投票

This should do it for the first matrix
A = zeros(4,4);
A(:,1) = 1; % set column 1 to 1
A(:,3) = 1; % set column 3 to 1
disp(A)
Im sure that you can use this example to do it for the second example

1 件のコメント

lamiae hmimou
lamiae hmimou 2020 年 5 月 16 日
thank you so much , that helped ! i'll use the same method to solve the other ones .. Thanks again

サインインしてコメントする。

DGM
DGM 2021 年 8 月 31 日
編集済み: DGM 2021 年 8 月 31 日

0 投票

How about an example using concatenation only. No arithmetic composition or array indexing.
e = ones(2);
c = zeros(3,2);
n = ones(1,2);
l = zeros(1,2);
m = ones(2,6);
s = zeros(11,2);
f = zeros(2,6);
result = [s [f; [[e; c] [c; n; l] [e; c]]; m; f] s]
result = 11×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
imshow(result)

5 件のコメント

M VENKATESH
M VENKATESH 2021 年 8 月 31 日
Using the zeros and ones commands create a 3 x 5 matrix in which the first second, and fifth columns are 0s, and the third and fourth columns are 1s.how can we create it?
DGM
DGM 2021 年 8 月 31 日
It can be created using concatenation as shown I intentionally chose to make my example demonstrate the concept instead of replicating the exact task. If you can assemble Lego bricks, then this shouldn't be too difficult to figure out.
To be honest, your assignment is extremely simple. In fact, it's simpler than the OP's simple assignment. It's so simple that even concatenation is overcomplicated. It could always be done without concatenation.
result = zeros(11,10);
result([3 4 8 9],[3 4 7 8]) = 1;
result([6 8 9],[5 6]) = 1
result = 11×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
imshow(result)
I suppose you could use ones(1) instead of 1 if you wanted to.
M VENKATESH
M VENKATESH 2021 年 8 月 31 日
Thank u so much
M VENKATESH
M VENKATESH 2021 年 8 月 31 日
Can u please sol this . Using the zeros and ones commands create a 3 x 5 matrix in which the first second, and fifth columns are 0s, and the third and fourth columns are 1s.
Walter Roberson
Walter Roberson 2021 年 8 月 31 日
hint:
A = [zeros(8,1), ones(8,1), zeros(8,1)]
A = 8×3
0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2020 年 5 月 16 日

コメント済み:

2021 年 8 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by