【问题标题】:concatenate columns of matrix into one vector将矩阵的列连接成一个向量
【发布时间】:2014-02-16 13:03:07
【问题描述】:

假设我们有以下两个随机矩阵

x=rand(7,7);

x

x =

    0.8147    0.5469    0.8003    0.0357    0.6555    0.8235    0.7655
    0.9058    0.9575    0.1419    0.8491    0.1712    0.6948    0.7952
    0.1270    0.9649    0.4218    0.9340    0.7060    0.3171    0.1869
    0.9134    0.1576    0.9157    0.6787    0.0318    0.9502    0.4898
    0.6324    0.9706    0.7922    0.7577    0.2769    0.0344    0.4456
    0.0975    0.9572    0.9595    0.7431    0.0462    0.4387    0.6463
    0.2785    0.4854    0.6557    0.3922    0.0971    0.3816    0.7094

y=rand(6,5)

y =

    0.7547    0.4984    0.2551    0.1386    0.2435
    0.2760    0.9597    0.5060    0.1493    0.9293
    0.6797    0.3404    0.6991    0.2575    0.3500
    0.6551    0.5853    0.8909    0.8407    0.1966
    0.1626    0.2238    0.9593    0.2543    0.2511
    0.1190    0.7513    0.5472    0.8143    0.6160

我想将每个矩阵的前四列连接成单个一维向量 c,我已经尝试过

c=[x(:,4) y(:,4)]
Error using horzcat
Dimensions of matrices being concatenated are not consistent.

但出现以下错误,请帮我解决这个问题

【问题讨论】:

  • 改用这个:c=[x(:,1:4); y(:,1:4)]; c(:)
  • 非常感谢您的帮助

标签: matlab matrix concatenation


【解决方案1】:
[ reshape(x(:,1:4),[],1); reshape(y(:,1:4),[],1) ]

这会将一列堆叠在另一列之下:首先是 x 的列,然后是 y 的列。我想这就是你想要的元素顺序。 (如果你想交错一列x,然后是y 之一等,请参阅@RobertP 的评论。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多