Multiply vector elements by their index (optimization question)

5 ビュー (過去 30 日間)
Scott
Scott 2011 年 6 月 22 日
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.

回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 6 月 22 日
%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 件のコメント
Scott
Scott 2011 年 6 月 23 日
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.
Scott
Scott 2011 年 6 月 23 日
Yes, it did.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTransforms についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by