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)... ?

1 次查看(过去 30 天)
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!!

采纳的回答

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

更多回答(2 个)

Matt Fig
Matt Fig 2012-11-15
编辑:Matt Fig 2012-11-15
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 2012-11-15
编辑:C.J. Harris 2012-11-15
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];

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by