Multiply vector elements by their index (optimization question)

5 visualizaciones (últimos 30 días)
Scott
Scott el 22 de Jun. de 2011
I'm looking to multiply elements of a vector by their respective index. Right now, I'm using the following code (variable names simplified and shortened):
for a = 0:Points-1
for b = 0:Points-1
ary1( a+1 ) = ary1( a+1 ) + ary2( b+1 ) * exp( i*a*b/Points );
I'd like to be able to change this into a vector operation in either direction, such as
for a = 0:Points-1
ary1( a+1 ) = ary1( a+1 ) + ary2 .* exp( i*a*b/Points );
The problem, of course, is that b no longer exists, nor can I make something that is a function of a that returns the appropriate b.
If it's relevant, the specific purpose of this code is DFT implementation. I do realize that MATLAB provides FFT as a built-in function, however, I need specific control that I can't get with the FFT functions provided with MATLAB.
Hopefully my question makes sense. Thank you for your time.

Respuestas (1)

Sean de Wolski
Sean de Wolski el 22 de Jun. de 2011
%Sample data
ary1 = (1:10).';
ary2 = rand(1,10);
Points = 10;
%Matrix Method
v = (0:(Points-1));
ary4 = ary1+sum(exp(1i*(v'*v)/Points)*ary2',2);
%Your method
for a = 0:Points-1
for b = 0:Points-1
ary1( a+1 ) = ary1( a+1 ) + ary2( b+1 ) * exp( 1i*a*b/Points );
end
end
%Check!
isclose = @(x,y)isequal(size(x),size(y))&&all(abs(x(:)-y(:))<10^-10);
isclose(ary1,ary4)
  2 comentarios
Scott
Scott el 23 de Jun. de 2011
Thanks for the quick reply.
There are two things, at least. The first is that it requires the same amount of points in both time and frequency domain representations of a signal.
The second is that is it requires a spectrum to start at 0, and when working with signals that have high frequency, this can make inconveniently large vectors.

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by