Combination of rows of two different matrices

S
Samantha · Jul 24, 2020 · 1.9K views
Question
Sorry if this is a repeated question but I failed to find an answer myself. I have two matrices:   A = [-0.6, -0.2; -60, 2; 6, -20]; B = [-0.4, -0.8; -40, 8; 4, -80]; I want to find all the possible combinations of sum of each row (sum of individual elements of a row) of A with each row of B, i.e., my desired result is (order does not matter): ans = [-1, -1; -40.6, 7.8; 3.4, -80.2; -60.4, 1.2; -100, 10; -56, -78; 5.6, -20.8; -34, -12; 10, -100]; which is a 9 x 2 matrix resulting from 9(=3 x 3 )possible combinations of A and B.
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 25, 2026

Feels non-ideal. Any better solution?

 

>> C=A(:,1)+B(:,1)';
D=A(:,2)+B(:,2)';
reshape([C(:),D(:)],[],2)
ans =
   -1.0000   -1.0000
  -60.4000    1.2000
    5.6000  -20.8000
  -40.6000    7.8000
 -100.0000   10.0000
  -34.0000  -12.0000
    3.4000  -80.2000
  -56.0000  -78.0000
   10.0000 -100.0000

better one

>> m=size(A,1);
ind1=repmat(1:m,1,m);
ind2=repelem(1:m,m);
A(ind1,:)+B(ind2,:)
ans =
   -1.0000   -1.0000
  -60.4000    1.2000
    5.6000  -20.8000
  -40.6000    7.8000
 -100.0000   10.0000
  -34.0000  -12.0000
    3.4000  -80.2000
  -56.0000  -78.0000
   10.0000 -100.0000
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!