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

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 Commenti

Do you need this generalized to any size matrix, or will it strictly be with three matrices of these exact sizes?
to any kind..although my specific problem is
A=64x1
B=64x1806
C=64x4602
so I need a generalized version

Accedi per commentare.

 Risposta accettata

[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 Commenti

I am trying this for
A=64x1
B=64x1806
C=64x4602
but getting error Maximum variable size allowed by the program is exceeded.
The result you're trying to produce appears to be too large, it's going to be around 64 x 4602*1806.
yes and each value is not a single integer but its like 2345+4583+2384 etc.Can I post the complete code?
So you need at least 4,26GB free memory. Since you do not have an error occurs.
means this calculation cannot be done
Perhaps make a post with your data asking someone to run the computation for you (requiring as Niels said, 4.26 GB). I have a laptop, but I'm certain some people have much better devices.
in total u need more than 4,26GB, 4,26GB only for the result to be saved in the memory, but the other variables are not to mention in comparison to result
ah you even may have the required memory but maybe you just need to set matlabs limit higher...
Requested 64x8311212 (4.0GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive.
I'm posting another question for trying to minimize the calculations in my code so that these numbers can be minimized
that error occurs even before you even start your calculations
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.

Accedi per commentare.

Più risposte (1)

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 Commenti

You would need to save it as a function then call,
D = strangelyAddingMatrices(A,B,C);
from the command window.
ok I'm trying this. let you know about it
Alternatively, you can copy and paste the body of the function to the command window:
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
or you could use Neils solution below which also works!
getting error of applying + on matrices
That's strange, I ran it on randomly generated matrices of the same size as yours and it worked, although it slowed my computer way down as it requires so much memory (as was mentioned below).
how can I reduce the memory consumption?
Other than running it on a better device or asking someone to run it for you, I can only think of computing it by block.
In other words, how will you use this resultant matrix? If you are only going to use a piece of it at a time, then you can compute the necessary subblock of that matrix as needed. Or you can intelligently generate a portion at a time as required for calculations, then overwrite that portion to have eventually used the whole matrix in the calculation.
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
What do you mean by minimum sum? Like which columns summed produce the minimum? Or something else?

Accedi per commentare.

Categorie

Prodotti

Richiesto:

il 24 Gen 2017

Commentato:

il 24 Gen 2017

Community Treasure Hunt

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

Start Hunting!

Translated by