how to add 3 matrices with one dimension same for all and other dimension different?

조회 수: 3 (최근 30일)
If I have
A=[1;
2;
3]
B=[2 3 4;
5 6 7;
8 9 0]
C=[3 4 5 6;
9 8 7 6;
5 4 2 1]
all 3 matrices have 3 rows but columns different. I want to add elements of rows of all matrices in this way for example I want
D=[(1+2+3) (1+2+4) (1+2+5) (1+2+6) (1+3+3) (1+3+4) (1+3+5) (1+3+6) (1+4+3) (1+4+4) (1+4+5) (1+4+6);
(2+5+9) (2+5+8) (2+5+7) (2+5+6) (2+6+9) (2+6+8) (2+6+7) (2+6+6) (2+7+9) (2+7+8) (2+7+7) (2+7+6);
(3+8+5) (3+8+4) (3+8+2) (3+8+1) (3+9+5) (3+9+4) (3+9+2) (3+9+1) (3+0+5) (3+0+4) (3+0+2) (3+0+1)]
  댓글 수: 2
John Chilleri
John Chilleri 2017년 1월 24일
Do you need this generalized to any size matrix, or will it strictly be with three matrices of these exact sizes?
summyia qamar
summyia qamar 2017년 1월 24일
to any kind..although my specific problem is
A=64x1
B=64x1806
C=64x4602
so I need a generalized version

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

채택된 답변

Niels
Niels 2017년 1월 24일
편집: Niels 2017년 1월 24일
[rows,col_B]=size(B);
[~,col_C]=size(C);
result=zeros(rows,col_B*col_C);
for i=1:col_B
for j=1:col_C
result(:,(i-1)*col_C+j)=A+B(:,i)+C(:,j);
end
end
  댓글 수: 11
Niels
Niels 2017년 1월 24일
that error occurs even before you even start your calculations
Niels
Niels 2017년 1월 24일
the result will be that large, so even if the calculations are minimzed, the problem with the size of the result is still the same.

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

추가 답변 (1개)

John Chilleri
John Chilleri 2017년 1월 24일
편집: John Chilleri 2017년 1월 24일
Hello,
I only tested this with your example and it worked, but I think it's generally correct:
%
% Strangely adding matrices
%
function D = strangelyAddingMatrices(A,B,C)
n = size(C,2);
nB = size(B,2);
for i = 1:n
C(:,i) = C(:,i)+A;
end
D = [];
for i = 1:nB
C2 = C;
for j = 1:n
C2(:,j) = C2(:,j)+B(:,i);
end
D = [D C2];
end
end
Hope this helps!
  댓글 수: 9
summyia qamar
summyia qamar 2017년 1월 24일
actually these A,B,C are resultants of different calculations and I want to get minimum sum of all A,B,C thats why I want to sum up them
John Chilleri
John Chilleri 2017년 1월 24일
What do you mean by minimum sum? Like which columns summed produce the minimum? Or something else?

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

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by