MATLAB Answers

I have to create a matrix using only zeros, ones and eye

73 views (last 30 days)
sarah beituni
sarah beituni on 21 Jan 2019
Commented: Kevin Phung on 21 Jan 2019
This is the matrix:
1 1 1 1 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
so far I wrote B=ones(1,5); C=eye(4,5); A=[B;C]
But this command didn't come out right. Can someone please help me.

  0 Comments

Sign in to comment.

Accepted Answer

Kevin Phung
Kevin Phung on 21 Jan 2019
Edited: Kevin Phung on 21 Jan 2019
your command A = [B;C] concatenated them vertically.
Try:
C = eye(5)
C(1,:) = 1; % turns every element along the first row to 1.

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 21 Jan 2019
>> C = eye(5);
>> C(1,:) = 1
C =
1 1 1 1 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Kevin Phung
Kevin Phung on 21 Jan 2019
I would like to clarify that I made the exact same changes right before I saw your comment!
Originally I had something like
C(1,:) = B;

Sign in to comment.

More Answers (1)

Rob Purser
Rob Purser on 21 Jan 2019
A = eye(5);
A(1,:) = ones(1,5);

  0 Comments

Sign in to comment.

Sign in to answer this question.