MATLAB Answers

Create Matrix using only zeros and ones

63 views (last 30 days)
lamiae hmimou
lamiae hmimou on 16 May 2020
Commented: Walter Roberson on 31 Aug 2021
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 Comments

Sign in to comment.

Answers (2)

William Alberg
William Alberg on 16 May 2020
Edited: William Alberg on 16 May 2020
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 Comment
lamiae hmimou
lamiae hmimou on 16 May 2020
thank you so much , that helped ! i'll use the same method to solve the other ones .. Thanks again

Sign in to comment.


DGM
DGM on 31 Aug 2021
Edited: DGM on 31 Aug 2021
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 Comments
Walter Roberson
Walter Roberson on 31 Aug 2021
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

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!