Build matrix with other matrices.
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
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?
回答(2 个)
Daniel Shub
2012-4-26
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
2012-5-11
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!