I am looking for a fast and flexible way to compute the following in Matlab without using a loop:
c = 1:5;
A = reshape(1:5^3,5,5,5);
res= c(1)*A(:,:,1)+...+c(5)*A(:,:,5)
I think, working with
sum(A,3)
could be a nice way as long as I am able to perform the multiplication along the third dimension. One solution (but with loops) would be:
val = zeros(length(c),length(c))
for i = 1:length(c)
val = val+c(i)*A(:,:,i)
end
I am just wondering if this can be done in a simpler (and more elegant) way avoiding the loop.