Build matrix with other matrices.

Perhaps this is a simple question, but I need an answer.
If I want to build a 6x8 matrix with help of 6 other matrices, is there a smart command or function to do that? I have only built matrix with vectors so far.
Anyhow, here is my attempt so far:
A=[3 4 2; 7 2 1]
B=[2 2 1; 5 2 8]
C=[4 2 1; 3 1 2; 0 7 1]
D=[1 2 4]
F=[3; 2; 1]
G=[2; 1; 4]
blkdiag(A,B,C,D,F,G)
ans =
Columns 1 through 6
3 4 2 0 0 0
7 2 1 0 0 0
0 0 0 2 2 1
0 0 0 5 2 8
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Columns 7 through 12
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
4 2 1 0 0 0
3 1 2 0 0 0
0 7 1 0 0 0
0 0 0 1 2 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Columns 13 through 14
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
3 0
2 0
1 0
0 2
0 1
0 4
>>
I got answer, but I wonder if this is correct or not.
I would be glad if you can help me
Best regards Cillian

댓글 수: 6

Sean de Wolski
Sean de Wolski 2012년 4월 26일
What result do you expect for the above? Demonstrate it with just two or three matrices.
Daniel Shub
Daniel Shub 2012년 4월 26일
I have a hard time seeing a reasonable way of combing your matrices A-G and getting a 6x8 matrix.
Sean de Wolski
Sean de Wolski 2012년 4월 26일
@Daniel: The only way I see it
X = zeros(6,8);
X(1:size(A,1),1:size(A,2)) = X(1:size(A,1),1:size(A,2))+A;
X(1:size(B,1),1:size(B,2)) = X(1:size(B,1),1:size(B,2))+B;
...
Cillian
Cillian 2012년 5월 11일
Thank you Sean, but can you please describe in words how it works for me. I don't quite understand it.
Like this one; " X(1:size(A,1),1:size(A,2)) = X(1:size(A,1),1:size(A,2))+A; "
Daniel Shub
Daniel Shub 2012년 5월 11일
Both Sean and I are confused about what you are asking. Does Sean's "answer" give you the result you are looking for?
Cillian
Cillian 2012년 5월 11일
Yes, Sean's answer give me what I am looking for. I just don't quite understand how his code works. Can you or Sean describe for me in words, please.

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

답변 (2개)

Daniel Shub
Daniel Shub 2012년 4월 26일

1 개 추천

Following on from Sean's comment, I was curious if I could do his calculation in a "cleaner" way that also uses blkdiag which Cillian seems to think is needed.
x = {A,B,C,D,F,G};
sum(reshape(cell2mat(cellfun(@(y)(blkdiag(y, zeros(6-size(y, 1), 8-size(y, 2)))), x, 'UniformOutput', false)), 6, 8, 6), 3)
15 10 8 0 0 0 0 0
18 5 11 0 0 0 0 0
5 7 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
EDIT The high level over view of the code is I make a 6x8xN matrix, where N is 6 because you have 6 vectors you are working with. I then sum across the 3rd dimension to get the answer. So working inside out with the code:
blkdiag(y, zeros(6-size(y, 1), 8-size(y, 2)))
produces a 6x8 matrix for any y matrix (that has size less than 6x8) where the values of y are in the top left corner of the matrix and zeros are in the remaining slots.
cellfun(@(y)(blkdiag(y, zeros(6-size(y, 1), 8-size(y, 2)))), x, 'UniformOutput', false)
produces a 1x6 cell array with each element of the cell array is a 6x8 version of the corresponding input matrices A, B, C, D, F.
cell2mat(cellfun(@(y)(blkdiag(y, zeros(6-size(y, 1), 8-size(y, 2)))), x, 'UniformOutput', false))
then converts the 1x6 cell array into a 6x48 matrix where the first 6x8 elements are from the 6x8 version of A and the next 6x8 elements are from the 6x8 version of B ...
Finally
reshape(cell2mat(cellfun(@(y)(blkdiag(y, zeros(6-size(y, 1), 8-size(y, 2)))), x, 'UniformOutput', false)), 6, 8, 6)
produces the desired 6x8x6 matrix and the final sum does the summation.
Andrei Bobrov
Andrei Bobrov 2012년 5월 11일

1 개 추천

z = {A,B,C,D,F,G};
out = zeros(6,8);
for jj = 1:numel(z)
[i1 j1] = size(z{jj});
out(1:i1,1:j1) = out(1:i1,1:j1) + z{jj};
end
or:
[I, J, c] = cellfun(@(x)find(x),z,'un',0);
k = reshape(cellfun(@(x)x(:),[I, J, c],'un',0),[],3);
out = accumarray(cell2mat(k(:,1:2)),cat(1,k{:,3}),[6, 8]);

댓글 수: 1

Daniel Shub
Daniel Shub 2012년 5월 11일
But you didn't use BLKDIAG. You do get points for using ACCUMARRAY, but I don't think the tag was there when I posted my answer.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2012년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by