0

I have matrices A=2x2 B=2x4 C=2x2 I want result in a matrix D=2x16 for example

A=[1 3;
   2 4]
B=[3 2 4 2;
   4 3 6 3]
C=[4 5;
   7 5]
D=[(1+3+4) (1+3+5) (1+2+4) (1+2+5) (1+4+4) (1+4+5) (1+2+4) (1+2+5) (3+3+4) (3+3+5) (3+2+4) (3+2+5) (3+4+4) (3+4+5) (3+2+4) (3+2+5);
   (2+4+7) (2+4+5) (2+3+7) (2+3+5) (2+6+7) (2+6+5) (2+3+7) (2+3+5) (4+4+7) (4+4+5) (4+3+7) (4+3+5) (4+6+7) (4+6+5) (4+3+7) (4+3+5)]

means that row wise elements are added in resultant. I have a code in which A=2x1 but I can understand how to mold it according to A=2x2 the code is given below

[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

can anybody tell me the syntax for the output RESULT in this code?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
summyia haris
  • 121
  • 1
  • 15
  • Can you please be a little more precise about what you want to be added? You say you want A1+B1+C1 .... A1+B1+C16, but in your example you do something different. In your example it's A1+B1+C1 .... A1+B1+C4, A1+B2(!!!)+C5 .... A1+B2+C8, A1+B3(!!!)+C9 .... A1+B3+C12 – Max Feb 14 '17 at 15:09
  • please check the edited question – summyia haris Feb 14 '17 at 15:14
  • couldn't understand your question – summyia haris Feb 14 '17 at 16:57
  • row 1 of D shows that 1st row of A is added to 1st row of B and C in such a way each element is added to all possible combinations of B and C – summyia haris Feb 14 '17 at 17:02

1 Answers1

1

Try

D = kron(A,ones(1,size(B,2)*size(C,2)))+repmat(kron(B,ones(1,size(C,2))),1,size(A,2))+repmat(C,1,size(B,2)*size(A,2))

Explained: kron(Mat, ones(1,num2RepeatColumns)), repeats each column of the matrix, Mat. So "repeating" twice makes A = [1 2; 3 4] becomes A = [1 1 2 2; 3 3 4 4], see Matlab: repeat every column sequentially n times

repmat(Mat, 1, num2RepeatMatrix) copies the whole matrix, Mat and "pastes" it right next to the original, so A=[1 2; 3 4] becomes A = [1 2 1 2; 3 4 3 4]. See "Horizontal Stack" example of repmat: https://www.mathworks.com/help/matlab/ref/repmat.html

OLD

D=[];
for ii=1:size(A,2)
    D=[D,A(:,ii)+kron(B,ones(1,size(C,2)))+repmat(C,1,size(B,2))];
end
Community
  • 1
  • 1
  • Well, the `repelem` repeats each column of B, how many times? the # of C columns. The `repmat` repeats the whole matrix C, how many times? the # of B columns. So those are kind of like 'hidden' loops. Google each function, or I can explicitly show you in an edit - but first make sure it's the answer you want - I think you have a typo in your D above. –  Feb 14 '17 at 15:31
  • Try now? I replaced the repelem –  Feb 14 '17 at 15:47
  • is this applicable to any dimensions of matrices with same number of rows? because its giving error that using + Matrix dimensions must agree.my actual problem contains A=8x16,B=8x192 and C=8x16 – summyia haris Feb 14 '17 at 15:52
  • tried but failed. can I send you my code If you can understand that? – summyia haris Feb 14 '17 at 16:03
  • I don't have access to matlab right now, if you do, `size(A(:,1)), size(kron(B,ones(1,size(C,2)))), size(repmat(C,1,size(B,2)))`, and let me know the answers, I should be able to fix it. –  Feb 14 '17 at 16:07
  • size(A(:,1))=`8`; size(kron(B,ones(1,size(C,2))))=` 8x3072` size(repmat(C,1,size(B,2))) `8x 3072` according to my problem size – summyia haris Feb 14 '17 at 16:12
  • So your A variable, seems to be 16x8, not 8x16... try tossing in an `A=A';` before the above code –  Feb 14 '17 at 16:14
  • Then your `size(A)`, would come out as `Column 1: 8, Column 2: 16` - is that the case? –  Feb 14 '17 at 16:17
  • again dimension error `size(kron(A,ones(1,size(B,2)*size(transport_cost,2)))) ans = Column 1 8 Column 2 49152 >> size(kron(B,ones(1,size(C,2)))) ans = Column 1 8 Column 2 3072 >> size(repmat(C,1,size(B,2))) ans = Column 1 8 Column 2 3072 ` – summyia haris Feb 14 '17 at 17:20
  • I checked with a test case, given the dimensions (`A=rand(8,16);B=rand(8,192);C=rand(8,16);`) and the above one-liner works. If the order is off, sorry for my misunderstanding, but seemed right. –  Feb 14 '17 at 18:05