element wise concatenation of square matrices

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)

Réponses (4)

Azzi Abdelmalek
Azzi Abdelmalek le 12 Fév 2013
Modifié(e) : Azzi Abdelmalek le 12 Fév 2013
n=numel(A)
out=arrayfun(@(x) [A(x) B(x)],1:n,'un',0)

5 commentaires

zozo
zozo le 12 Fév 2013
Modifié(e) : zozo le 12 Fév 2013
@Azzi, Thank you. But I want both the elements as a string in a cell. In your solution, if I go inside each cell, I see the elements as a row vector of size (1x2). I want both the elements to be concatenated in a single dimension. The final cell should also be of size C 3x3.
Please help
Azzi Abdelmalek
Azzi Abdelmalek le 12 Fév 2013
Modifié(e) : Azzi Abdelmalek le 12 Fév 2013
What do you mean: as a string? '1718'? or what?
Azzi Abdelmalek
Azzi Abdelmalek le 12 Fév 2013
Modifié(e) : Azzi Abdelmalek le 12 Fév 2013
out=arrayfun(@(x) num2str([A(x) B(x)]),1:n,'un',0)
zozo
zozo le 12 Fév 2013
Modifié(e) : zozo le 12 Fév 2013
Yes..Like
17 18 24 27 1 35
23 2 65 5 7 8
4 4 6 16 13 11
Ok, what about
out=arrayfun(@(x,y) num2str([x y]),A,B,'un',0)

Connectez-vous pour commenter.

Thorsten
Thorsten le 12 Fév 2013
Modifié(e) : Thorsten le 12 Fév 2013
Like this?
C = arrayfun(@(x) ['<' num2str(A(x)) ',' num2str(B(x)) '>'], 1:numel(A), 'un', 0);
C = reshape(C, [3 3]);
zozo
zozo le 12 Fév 2013
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. :-)

2 commentaires

out=arrayfun(@(x,y) num2str([x y]),A,B,'un',0)
zozo
zozo le 12 Fév 2013
Thanks Azzi !!

Connectez-vous pour commenter.

Catégories

Question posée :

le 12 Fév 2013

Community Treasure Hunt

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

Start Hunting!

Translated by