Ryan Hubscher
Ryan Hubscher

Reputation: 81

How do I enter a flipped toeplitz matrix in matlab?

What is a clean way to use vector a to make matrix A in matlab? Notice that the bottom right corner is padded with ones. Sometimes I will want to pad it with something else, like zeros.

% use this vector
a = [4, 2, 5, 6]

% to make this matrix
A = [
    4, 2, 5, 6;
    2, 5, 6,     1;
    5, 6,     1, 1]

I tried using combinations of flip and toeplitz. I succeeded, but it got messy.

Upvotes: -1

Views: 54

Answers (2)

Ryan Hubscher
Ryan Hubscher

Reputation: 81

I used the hankel() function.

This works, but produces a warning:

A = hankel(a, ones(1,3))'

This is longer, but has no warnings:

A = hankel(a, [a(end) ones(1,2)])'

Andras Deak gave us the right answer in his comment.

Upvotes: -1

John Bofarull Guix
John Bofarull Guix

Reputation: 820

a=randi([-10 10],1,4)  % input 1D vector
A1=toeplitz(a);
A2=flip(A2,1);
A3=triu(toeplitz(A2)) + tril(ones(size(A2)),-1);
A3=flip(A3,1);
A3(end,:)=[]   % result

Upvotes: -1

Related Questions