Clear Filters
Clear Filters

Build matrix with other matrices.

9 views (last 30 days)
Cillian
Cillian on 26 Apr 2012
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 Comments
Daniel Shub
Daniel Shub on 11 May 2012
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 on 11 May 2012
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.

Sign in to comment.

Answers (2)

Daniel Shub
Daniel Shub on 26 Apr 2012
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 on 11 May 2012
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 Comment
Daniel Shub
Daniel Shub on 11 May 2012
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.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!