Fastest pairwise row sums
Show older comments
I have two matrices A and B, and I would like to compute a matrix C whose rows are all possible combinations of the sum of a row of A and a row of B. What would be the fastest way to perform the computation?
Additional question - what if A and B are both gpuArrays?
Update - Example
Say A is a matrix of 3 rows and B is a matrix of 2 rows, then I would like C to be the matrix
C = [A(1, :) + B(1, :);
A(1, :) + B(2, :);
A(2, :) + B(1, :);
A(2, :) + B(2, :);
A(3, :) + B(1, :);
A(3, :) + B(2, :)];
2 Comments
madhan ravi
on 26 Feb 2019
Edited: madhan ravi
on 26 Feb 2019
illustrate with a short example
Alexander Shtof
on 26 Feb 2019
Accepted Answer
More Answers (1)
Jos (10584)
on 26 Feb 2019
% An old-school 20th century indexing trick:
A = randi(9,3,2)
B = 100*randi(9,4,2)
[ia, ib] = ndgrid(1:size(A,1),1:size(B,1))
C = A(ia,:) + B(ib,:)
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!