Reputation: 21
Here is the question: Write a MATLAB program which uses the diag command to generate n by n circulant matrices: given vector v = (v1 v2 v3 ....vn). Example of circulant matrices:
A = [1 2 3 4 5;
5 1 2 3 4;
4 5 1 2 3;
3 4 5 1 2;
2 3 4 5 1]
note the wrap-around patter of successive rows.
Use diag command in MATLAB
.
(here is what I started from, I am having trouble to construct the matrix using diag
command. Please help).
n = length(v);
a = zeros(n);
for i = 1:n
Upvotes: 0
Views: 2295
Reputation: 933
The simplest way to do this without any looping is to take advantage of the Fourier properties of circulant matrices and use the FFT:
V = ifft( fft( diag( fft(x) ), [], 1 ), [], 2 );
Upvotes: 0
Reputation: 225
Interestingly, since all the circulant matrices are made diagonal by the DFT, you can also do something like that
// given vector
v = [1 2 3 4 5];
n = numel(v);
// DFT matrix
F = dftmtx(n);
//circulant matrix
V = (F*diag(fft(v))*F')./n;
Upvotes: 3
Reputation: 11
function m = prgrm9( x )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
m = diag(x(1)* ones(1,length(x)));
for i = 2: length(x)
m = m + diag(x(i) * ones(1, length(x)-(i-1)),i-1)+ diag(x(i)*ones(1,i-1),i-(length(x)+1));
end
Upvotes: 1
Reputation: 221624
Code
%%// Given vector v
v = [1 2 3 4 5];
%%// Parameters
n = numel(v);
arr1 = [v1(2:end) v1];
%%// Construct circulant matrix
A1 = zeros(n);
for k = -(n-1):n-1
A1 = A1+ arr1(k+n)*diag(ones(n-abs(k),1),k);
end
Upvotes: 1