Given matrices XX and YY of sizes 3X3, how can I generate the following matrix: [XX(1,1) YY(1,1); XX(1,2) YY(1,2)... ?

4 views (last 30 days)
Given matrices XX and YY of sizes 3X3, how can I generate the following matrix:
[XX(1,1) YY(1,1); XX(1,2) YY(1,2);XX(1,3) YY(1,3); XX(2,1) YY(2,1); XX(2,2) YY(2,2); XX(2,3) YY(2,3); XX(3,1) YY(3,1); XX(3,2) YY(3,2); XX(3,3) YY(3,3)]
I mean of course, without writing it explicitly and without loops.
Thanks!!

Accepted Answer

C.J. Harris
C.J. Harris on 15 Nov 2012
or:
XX = magic(3);
YY = eye(3);
XX_trans = transpose(XX);
YY_trans = transpose(YY);
out = [XX_trans(:) YY_trans(:)];

More Answers (2)

Matt Fig
Matt Fig on 15 Nov 2012
Edited: Matt Fig on 15 Nov 2012
For example:
XX = magic(3);
YY = cumsum(ones(3));
% Your requested matrix.
ZZ = [reshape(XX.',[],1) reshape(YY.',[],1)]

C.J. Harris
C.J. Harris on 15 Nov 2012
Edited: C.J. Harris on 15 Nov 2012
This is one way of doing it - expanding on my answer to your previous question:
XX = magic(3);
YY = eye(3);
out = unique(nchoosek([1:3, 1:3], 2), 'rows');
XX_Array = arrayfun(@(x)[(XX(out(x,1),out(x,2)))], 1:size(out,1))';
YY_Array = arrayfun(@(x)[(YY(out(x,1),out(x,2)))], 1:size(out,1))';
combArray = [XX_Array YY_Array];

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!