필터 지우기
필터 지우기

Adding each row of a matrix to another matrix

조회 수: 2 (최근 30일)
SciFiPhysics Guy
SciFiPhysics Guy 2022년 4월 21일
댓글: SciFiPhysics Guy 2022년 4월 22일
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
Image Analyst
Image Analyst 2022년 4월 21일
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 2022년 4월 21일
편집: SciFiPhysics Guy 2022년 4월 21일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 4월 21일
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
Voss
Voss 2022년 4월 22일
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 2022년 4월 22일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by