I have a task to find the maximum product of elements in a matrix. The input arguments are the matrix and the scalar n that tells me how many elements to multiply. If n=2, then I should multiply two by two. The maximum product can lie either in rows, or in columns on in diagonal. For example in this case A's elements were multiplied 2 by 2 along rows (B) and columns (C).
A =
8 1 6
3 5 7
4 9 2
B =
8.0000 6.0000
15.0000 35.0000
36.0000 18.0000
C =
24.0000 5.0000 42.0000
12.0000 45.0000 14.0000
I do it using the loop
c=[]
for ii = 1:(length(A)-n+1)
p = prod(A(:,ii:ii+n-1));
c=[c p];
end
or
for i=1:size(A,2)
B(i,:)=real(exp(conv(log(A(i,:)),ones(1,n),'valid')));
C(:,i)=real(exp(conv(log(A(:,i)),ones(1,n),'valid')));
end
In both cases I receive the product but when it comes to getting the maximum among that products, (in my case that is the product of A(2,2)*A(3,2)=45) I cannot find the indices of original matrix elements that formed that product, i.e. (2,2) and (3,2).
Any help will be appreciated.