How to dynamically create multiple column vectors?
6 views (last 30 days)
Show older comments
Hi there. I need to compute a matrix R which is computed the following way.
STEP 1: Create L number of column vectors which contains M number of elements
STEP 2: Multiply each column vector by it's transpose, obtaining a MxM matrix
STEP 3: Find the sum of adding all the matrices found in step 2.
My questions are the following:
1) What is the best way to use a for loop to create the column vectors i need?
2) How do I take elements from the end of one array and add them to my new array? (see comment in code below)
M = 5; % Number or elements in each vector
L = 1000; % Number of column vectors
N = M+L;
a = 0.05;
x = cos(2*pi*0.2*(0:N-1)) + cos(2*pi*0.38*(0:N-1))+(a*randn(1,N));
d = 0.4*cos(2*pi*0.2*(0:N-1)) + (pi/5);
% Compute R
% STEP 1: Create L number of column vectors which contains M number of elements
% STEP 2: Multiply each column vector by it's transpose, obtaining a MxM
% matrix
% STEP 3: Find the sum of the matrices found in step 2.
% x0 = [x(1) x(end) x(end-1) x(end-2) x(end-3)].transpose
% x1 = [x(2) x(1) x(end) x(end-1) x(end-2)].transpose
% x2 = [x(3) x(2) x(1) x(end) x(end-1)].transpose
% x3 = [x(4) x(3) x(2) x(1) x(end)].transpose
% x4 = [[x(5) x(4) x(3) x(2) x(1)].transpose
% x(L-1) = [x(L) x(L-1) x(L-2) x(L-3) x(L-4)].
0 Comments
Accepted Answer
DGM
on 23 Feb 2022
Edited: DGM
on 23 Feb 2022
How about something like this?
M = 5; % Number or elements in each vector
L = 1000; % Number of column vectors
N = M+L;
a = 0.05;
x = cos(2*pi*0.2*(0:N-1)) + cos(2*pi*0.38*(0:N-1))+(a*randn(1,N));
d = 0.4*cos(2*pi*0.2*(0:N-1)) + (pi/5);
% Compute R
% STEP 1: Create L number of column vectors which contains M number of elements
% STEP 2: Multiply each column vector by it's transpose, obtaining a MxM
% matrix
% STEP 3: Find the sum of the matrices found in step 2.
% x0 = [x(1) x(end) x(end-1) x(end-2) x(end-3)].transpose
% x1 = [x(2) x(1) x(end) x(end-1) x(end-2)].transpose
% x2 = [x(3) x(2) x(1) x(end) x(end-1)].transpose
% x3 = [x(4) x(3) x(2) x(1) x(end)].transpose
% x4 = [[x(5) x(4) x(3) x(2) x(1)].transpose
% x(L-1) = [x(L) x(L-1) x(L-2) x(L-3) x(L-4)].
idxx = N:-1:1;
xksum = 0;
for k = 1:L
idxx = circshift(idxx,1); % shift the index vector
xk = x(idxx(1:M)); % extract a sample from x
xk = xk.*xk.'; % multiply
xksum = xksum + xk; % add to total
end
xksum % show the result
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!