smultiplication of matrix element

If I have a matrix A, 5X8 matrix, how can I generate a new matrix B 5X4, in which the first column is the product of the first two columns of A, (element by element), the second column is the product of column 3 and 4 of A, and soon. Of course, i am interested in an efficient code because I will have to work with a very large matrix. Please advise any thoughts are greatly appreciated. Many thanks

 Accepted Answer

A = randi(125,5,8);
out = A(:,1:2:end).*A(:,2:2:end);
variant use loop
outc = zeros(5,4);
for j1 = 1:size(outc,2)
j2 = j1*2;
outc(:,j1) = A(:,j2-1).*A(:,j2);
end
exotic :)
oute = squeeze(prod(reshape(A,size(A,1),2,[]),2));

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!