How to cycle through a vector to create a matrix?

A=[a,b,c,d,0,0,0,0...,0] 1xN vector
Using vector "A", I want to create an NxN matrix such that each row is the previous row with its elements fliped one at a time. That is, I want to create:
[a,b,c,d,0,0,0,...,0;
0,a,b,c,d,0,0,,...0;
0,0,a,b,c,d,0,...0;
.....
0,...0,0,0,a,b,c,d]
I could use a "for statement" using A=[A(end), A(1:end-1)] and vertcat(). But is there a more efficient and elegant way of doing this? Thanks

 Accepted Answer

I am not certain what result you want.
Try this:
syms a b c d
N = 10-4;
Av = [a b c d 0 0 0 0 0 0];
for k = 0:N
A(k+1,:) = circshift(Av,k,2)
end
producing:
A =
[a, b, c, d, 0, 0, 0, 0, 0, 0]
[0, a, b, c, d, 0, 0, 0, 0, 0]
[0, 0, a, b, c, d, 0, 0, 0, 0]
[0, 0, 0, a, b, c, d, 0, 0, 0]
[0, 0, 0, 0, a, b, c, d, 0, 0]
[0, 0, 0, 0, 0, a, b, c, d, 0]
[0, 0, 0, 0, 0, 0, a, b, c, d]
.

2 Comments

Thanks. That is beautiful.
As always, my pleasure!
Thank you!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!