How to dynamically create multiple column vectors?

6 visualizaciones (últimos 30 días)
Justin Goh
Justin Goh el 23 de Feb. de 2022
Comentada: Justin Goh el 23 de Feb. de 2022
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)].

Respuesta aceptada

DGM
DGM el 23 de Feb. de 2022
Editada: DGM el 23 de Feb. de 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
xksum = 5×5
1.0e+03 * 1.0014 -0.2122 -0.3699 -0.0845 -0.3427 -0.2122 1.0018 -0.2123 -0.3695 -0.0844 -0.3699 -0.2123 1.0013 -0.2122 -0.3699 -0.0845 -0.3695 -0.2122 1.0018 -0.2120 -0.3427 -0.0844 -0.3699 -0.2120 1.0015

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by