How to dynamically create multiple column vectors?

3 Ansichten (letzte 30 Tage)
Justin Goh
Justin Goh am 23 Feb. 2022
Kommentiert: Justin Goh am 23 Feb. 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)].

Akzeptierte Antwort

DGM
DGM am 23 Feb. 2022
Bearbeitet: DGM am 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
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

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by