Multiply vector elements by their index (optimization question)
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Sean de Wolski
le 22 Juin 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)
Voir également
Catégories
En savoir plus sur Transforms dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!