Multiply vector elements by their index (optimization question)
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Transforms 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!