double summation without a loop
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Leo Simon
el 29 de Mzo. de 2015
Comentada: Jan
el 30 de Mzo. de 2015
I have an anonymous function f that has two indices, i and t. i runs from 1 to n; t runs from 1 to m != n. For example:
f = @(delta,i,t) exp( - delta(i).*t );
delta = rand(1:n);
Obviously, I can sum f with respect to either t or i,
sum(f(delta,1:n,t))
sum(f(delta,i,1:m))
But I'd like to compute, in one step, sum_i=1^n (sum_t=1^m f(delta,i,t)) , without writing a loop. Is this possible? thanks very much for any advice!
0 comentarios
Respuesta aceptada
Jan
el 29 de Mzo. de 2015
f = @(delta,i,t) exp(bsxfun(@times, -delta(i), t.'))
result = sum(reshape(f(delta, 1:n, 1:m), 1, []))
3 comentarios
Stephen23
el 30 de Mzo. de 2015
Editada: Stephen23
el 30 de Mzo. de 2015
In general operations with a period character . are elementwise operations, whereas the operations without are standard linear algebra operations.
In this case:
- ' is the complex conjugate transpose of the matrix.
- .' is the non-conjugate tranpose of the matrix (elementwise transpose).
Jan Simon used the non-conjugate transpose in their answer, and this is the one you should use too.
Jan
el 30 de Mzo. de 2015
@Stephen: In their answer? I'm aware that the name Jan can be male and female and Simon is a common first name also, but we are at least only one person. So I'd prefer his ;-)
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Operations and Transformations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!