Multiply vector elements by their index (optimization question)
5 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Sean de Wolski
on 22 Jun 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)
See Also
Categories
Find more on Transforms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!