Add some elements of one matrix with another
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I need to add some elements of several ‘n’ square matrices in a particular fashion and append zeros in the rest of the elements.
Let’s say for example, there are 4 2 x 2 square matrices,
1st matrix = [1 2; 3 4]
2nd matrix = [5 6; 7 8]
3rd matrix = [9 10; 11 12]
4th matrix = [13 14; 15 16]
Then resultant matrix should be (5x5),
R = [1 2 0 0 0;
3 4+5 0+6 0 0;
0 0+7 0+8+9 0+10 0;
0 0 0+11 0+12+13 0+14;
0 0 0 0+15 0+16]
But if there are three 4 x 4 input matrices,
1st matrix = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
2nd matrix = [a b c d; e f g h; i j k l; m n o p]
3rd matrix = [A B C D; E F G H; I J K L; M N O P]
Then resultant matrix should be (8x8),
[1 2 3 4 0 0 0 0;
5 6 7 8 0 0 0 0;
9 10 11+a 12+b 0+c 0+d 0 0;
13 14 15+e 16+f 0+g 0+h 0 0;
0 0 i j k+A l+B 0+C 0+D;
0 0 m n o+E p+F 0+G 0+H;
0 0 0 0 0+I 0+J 0+K 0+L;
0 0 0 0 0+M 0+N 0+O 0+P]
So now it will go forward by 2 elements between the matrices and for 6*6 matrices, it will go forward by 3 elements between the matrices and so on.
The step for n matrices of size 2*2 is n+1 and 4*4 is 2(n+1) and 6*6 is 3(n+1) and so on.
So is there any way by which I can compute this resultant matrix for ‘n’ even square matrices?
Thanks in advance.
댓글 수: 4
Jan
2019년 4월 10일
@vanrapa: Do you see that it would have saved time, if you mention the exact needs in the original question already? The less the readers have to guess, the more efficient is posting an answer.
Now you mention, that "n + (w-1)" is not correct, but you still let us guess, why 3 matrices of size 4x4 will produce a 8x8 output. Explaining the pattern would be much better than posting the example, which is really hard to read:#
[1 2 3 4 0 0 0 0; 5 6 7 8 0 0 0 0; 9 10 11+a 12+b 0+c 0+d 0 0; 13 14 15+e 16+f 0+g 0+h 0 0; 0 0 i j k+A l+B 0+C 0+D; 0 0 m n o+E p+F 0+G 0+H; 0 0 0 0 0+I 0+J 0+K 0+L; 0 0 0 0 0+M 0+N 0+O 0+P]
In matrix notation this looks at least a little bit better:
[1 2 3 4 0 0 0 0;
5 6 7 8 0 0 0 0;
9 10 11+a 12+b 0+c 0+d 0 0;
13 14 15+e 16+f 0+g 0+h 0 0;
0 0 i j k+A l+B 0+C 0+D;
0 0 m n o+E p+F 0+G 0+H;
0 0 0 0 0+I 0+J 0+K 0+L;
0 0 0 0 0+M 0+N 0+O 0+P]
Do you see it? So now you go forward by 2 elements between the matrics. But what is the step for n matrics of size m*m?
채택된 답변
Stephen23
2019년 4월 9일
편집: Stephen23
2019년 4월 9일
Using a straightforward loop:
C = {[1,2;3,4],[5,6;7,8],[9,10;11,12],[13,14;15,16]};
A = cat(3,C{:}); % not strictly required, but ensures same size matrices.
S = size(A);
N = S(3)+S(2)-1; % adjust to suit step, etc.
B = zeros(N,N,S(3));
for k = 1:S(3)
V = k:k+S(1)-1;
B(V,V,k) = A(:,:,k);
end
Z = sum(B,3)
Giving:
Z =
1 2 0 0 0
3 9 6 0 0
0 7 17 10 0
0 0 11 25 14
0 0 0 15 16
댓글 수: 4
Stephen23
2019년 4월 10일
That is easy using Jan's concept:
C = {[1,2;3,4],[5,6;7,8],[9,10;11,12],[13,14;15,16]}; % four 2x2
C = {randi(99,4),randi(99,4),randi(99,4)}; % three 4x4
D = cellfun(@size,C,'uni',0);
assert(isequal(D{:}),'all matrices must have the same size')
assert(diff(D{1})==0,'all matrices must be square')
S = D{1};
P = numel(C);
N = (P+1)*S(1)/2;
Z = zeros(N,N);
for k = 1:P
V = (1:S(1)) + (k-1)*S(1)/2;
Z(V,V) = Z(V,V) + C{k};
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!