How to add matrices with different dimensions

hello, I want the shortest way to add matrices with different dimensions. I know the easiest way but it is not appropriate for big matrices like 10*10 or bigger please see the picture to understand what I mean Sorry there was a problem with the picture. I provide an example. I hope it is clear now.

6 Comments

How? post a short numeric example with the expected result
wha? Am i missing something? K1 looks to be 4x4 that has a mosaic of k sub nxm following row column numbering. K2 is a 4x4 with a mosaic of k sub nxm with a specified pattern. However a 4x4 +4x4 should still be a 4x4? how does it go to 6x6? or is that K=K1+K2 = 36?
Mohammed you don't need a picture to post an example
Hi Aziz The example in the picture Do you see the picture or not?
Mohammed my name is Azzi, and I meant, for your case, you don't need to add a picture, just write your example
Hi Azzi, sorry for writing your name wrongly and thank you for your advice.

Sign in to comment.

 Accepted Answer

That makes more sense now. you can do something like this.
k1=reshape([1:16],4,4)';
k2=reshape(17:32,4,4)';
K1=zeros(6,6);
K2=K1;
K1(1:length(k1),1:length(k1))= k1;
k2pattern= [5:6 1:2];
K2(k2pattern,k2pattern) = k2;
K=K1+K2
not the most efficient way nor did i supply how to vary depending on the size of the matrix. However this was <5 min of thinking without knowing what to with k2 for larger sizes. Just substitute the k2pattern with what its column labeling.

6 Comments

Thank you a lot. I think this is the best answer. I am still practicing to understand and I may need your help again. Thank you a lot. I appreciate your help.
Just run through it line by line and see the output.
Mohammed
Mohammed on 4 Apr 2014
Edited: Mohammed on 4 Apr 2014
Hi Joseph I run it and ot was correct, but I don't know how you did that Imagine you have three matrices, k1=4*4 (1 2 5 6) and k2=3*3 (4 1 2) k3=4*4 (3 4 7 8) k= k1 + k2 +k3=8*8 regardless of the values inside the matrices, could you please tell me how you will add them?
Very similar manner. As i did in the 2 matrices version i made a zero filled matrix for each with the size of the output labeling them as K#. in the capital K variable i did K1(indexpattern,indexpattern). and you show the indexing pattern in your text above. K1([1 2 5 6],[1 2 5 6]) = k1, K2(pattern,pattern)=k2. etc.
What is happening here is when i go K1(pattern,pattern) i'm specifying which rows and columns to put it in. The pattern I noticed from your examples allows us to go this route. Like i said before step through it line by line and look at how the K# are being formed. What is happening here is
As a off example example when i do:
A = zeros(10,10);
A([1 2 3 4],[5 6 7 8])= 1;
makes the rows 1,2,3and 4 the number one for columns 5,6,7 and 8. the indexes is the permutation of the two arrays and fills in what you say. (1,5),(1,6),(1,7)...(3,5),(3,6)...(4,8).
similarly if i go
A = zeros(10,10); A(1:2:end,2:2:end)=1
which will make every odd column 1 for every even column.
So for your example you have a 4x4 which when you look at the generic case K1 = some matrix filled with a permutation of the pattern.
Hi Joseph Sorry for my questions. I try to do it in MatLab and I cannot find the answer because the program said it is out of the dimensions could please show me how you create the matrix?

Sign in to comment.

More Answers (1)

Just extract all the values and add them.
K = zeros(6); % Initialize
K(1,1) = K1(1,1) + K2(3, 1); % Sum up k11 values.
K(1,2) = K1(1,2) + K2(3, 2); % Sum up k12 values.
and so on for all 36 values. It's not rocket surgery - just pluck them out of where they're defined to be in each matrix and add them together. Simple as that.

1 Comment

Thank you for the answer, but when I have 20*20 matrices, it will take time

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!