Clear Filters
Clear Filters

Adding each row of a matrix to another matrix

2 views (last 30 days)
Is there a linear operation for adding the individual rows of a matrix to another matrix, something like a tensor product but for summing? if not, would repmat be better here? This is my implimentation:
For a numeric Example:
A1 =rand(4, 2);
A2 =rand(2, 2);
A12=zeros(size(A1,1)*size(A2,1),size(A1,2));
startind=1+size(A1,1)*(0:(size(A2,1)-1));
endind=startind+size(A1,1)-1;
for i=1:size(A2,1)
A12(startind(i):endind(i),:)=A2(i,:)+A1;
end
For a symbolic Example:
A1 =sym('A1', [4 2]);
A2 =sym('A2', [2 2]);
A12=sym('A12', [size(A1,1)*size(A2,1),size(A1,2)]);
startind=1+size(A1,1)*(0:(size(A2,1)-1));
endind=startind+size(A1,1)-1;
for i=1:size(A2,1)
A12(startind(i):endind(i),:)=A2(i,:)+A1;
end
  2 Comments
Image Analyst
Image Analyst on 21 Apr 2022
I don't have the symbolic toolbox so I'm not sure what you're doing, but wouldn't it just be
A12 = A1 + A1;
Give a small numerical example so we can see what you're starting with for A1 and A2, and what you'd like to end up with for A12.
SciFiPhysics Guy
SciFiPhysics Guy on 21 Apr 2022
Edited: SciFiPhysics Guy on 21 Apr 2022
Thanks for the reply, I have included a numeric example. Maybe a better way to word the question is: Is there a linear operation to broadcast one row in a matrix to another matrix? So more like A2(1,:)+A1 then repeating that for each row in A2.

Sign in to comment.

Accepted Answer

Voss
Voss on 21 Apr 2022
A1 =rand(4, 2);
A2 =rand(2, 2);
% the original method
A12=zeros(size(A1,1)*size(A2,1),size(A1,2));
startind=1+size(A1,1)*(0:(size(A2,1)-1));
endind=startind+size(A1,1)-1;
for i=1:size(A2,1)
A12(startind(i):endind(i),:)=A2(i,:)+A1;
end
A12_original = A12;
% another method
A12 = repmat(A1,size(A2,1),1)+repelem(A2,size(A1,1),1);
% check
isequal(A12,A12_original)
ans = logical
1
% another method
[ii,jj] = meshgrid(1:size(A2,1),1:size(A1,1));
A12 = A2(ii,:)+A1(jj,:);
% check
isequal(A12,A12_original)
ans = logical
1
  3 Comments
Voss
Voss on 22 Apr 2022
I'm not sure about an analogy to kron but for summation, but here's another way to do the operation in question (where the matrices have the same number of columns and the summation is done with all pairs of rows), this time with no indexing or repmat/repelem:
A1 = rand(4,2);
A2 = rand(2,2);
A12 = reshape(permute(A1,[1 3 2])+permute(A2,[3 1 2]),[],size(A1,2));
A12 = 8×2
0.3120 0.5850 0.7758 0.4788 1.1809 0.9340 1.1380 0.4903 0.2080 1.0628 0.6717 0.9565 1.0768 1.4117 1.0339 0.9681
% check
A12_original = repmat(A1,size(A2,1),1)+repelem(A2,size(A1,1),1);
isequal(A12,A12_original)
ans = logical
1
SciFiPhysics Guy
SciFiPhysics Guy on 22 Apr 2022
That's great! I was just playing with the permute function and couldn't figure out the reshape portion of it. Thanks alot!
I'm thinking there isn't anything like kron for summing, but this permute function seems perfect.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!