MATLAB Answers

How to reconstruct a matrix?

2 views (last 30 days)
i have
x = [8, 9, -3, 9, 5, -4, -1, 3, 10, 10]
y = [-6; 4; 4; -2; 2]
z = diag(y)
how can i reconstruct matrix z into this look?
z =
-6 0 0 0 0 8
0 4 0 0 0 9
0 0 4 0 0 -3
0 0 0 -2 0 9
0 0 0 0 2 5
8 9 -3 9 5 -4
is there other way than this?
z = [-6 0 0 0 0 8; 0 4 0 0 0 9; 0 0 4 0 0 -3; 0 0 0 -2 0 9; 0 0 0 0 2 5; 8 9 -3 9 5 -4]
  1 Comment
Rik
Rik on 13 May 2021
Where are the lower values coming from? Do you just want to put the values of x on the lower and right edges?
What have you tried to put them there?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 May 2021
x = [8, 9, -3, 9, 5, -4, -1, 3, 10, 10];
y = [-6; 4; 4; -2; 2];
z = [diag(y), x(1:5).'; x(1:6)]
z = 6×6
-6 0 0 0 0 8 0 4 0 0 0 9 0 0 4 0 0 -3 0 0 0 -2 0 9 0 0 0 0 2 5 8 9 -3 9 5 -4
  2 Comments
Walter Roberson
Walter Roberson on 13 May 2021
z(4,:) = a(end-5:end);

Sign in to comment.

More Answers (1)

David Hill
David Hill on 13 May 2021
z=diag([y;0]);
z(end,:)=x(1:length(z));
z(:,end)=x(1:length(z));

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!