user466534
user466534

Reputation:

concatenate columns of matrix into one vector

suppose that we have following two random matrix

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

and

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

i want to concatenate first four columns of each matrix into single one dimensional vector c,i have tried

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

but get following error,please help me to solve this problem

Upvotes: 1

Views: 2655

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

[ reshape(x(:,1:4),[],1); reshape(y(:,1:4),[],1) ]

This stacks one column below the other: first those of x and then those of y. I guess that's the element order you want. (If you want to interleave one column of x, then one of y etc, see @RobertP's comment.)

Upvotes: 1

Related Questions