element wise concatenation of square matrices

1 次查看(过去 30 天)
Hi, I have the following two square matrices:
A=
17 24 1
23 65 7
4 6 13
B=
18 27 35
2 5 8
4 16 11
I want to concatenate the elements of matrices A and B to form a cell C such that:
C= <17,18> <24,27> <1,35>
<23,2> <65,5> <7,8>
<4,4> <6,16> <13,11>
Please help. (I want to avoid using for loop)

回答(4 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-2-12
编辑:Azzi Abdelmalek 2013-2-12
n=numel(A)
out=arrayfun(@(x) [A(x) B(x)],1:n,'un',0)
  5 个评论
zozo
zozo 2013-2-12
编辑:zozo 2013-2-12
Yes..Like
17 18 24 27 1 35
23 2 65 5 7 8
4 4 6 16 13 11

请先登录,再进行评论。


Thorsten
Thorsten 2013-2-12
编辑:Thorsten 2013-2-12
Like this?
C = arrayfun(@(x) ['<' num2str(A(x)) ',' num2str(B(x)) '>'], 1:numel(A), 'un', 0);
C = reshape(C, [3 3]);

Sean de Wolski
Sean de Wolski 2013-2-12
cellfun(@squeeze,num2cell(cat(3,A,B),3),'Uni',false)

zozo
zozo 2013-2-12
This is what I wanted.
A=magic(3);
B=magic(3);
C = arrayfun(@(x) [num2str(A(x)) ' ' num2str(B(x))],1:numel(A), 'un', 0);
Z=reshape(C,3,3)
Thanks for your help! Azzi, Thorsten, Sean. :-)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by