MATLAB Answers


trying to create a new vector from 2 vectors.

Asked by John
on 23 Mar 2012


I have a routine below that reads in an excel file. The vector y is created. It then computes r from a function and from that y1 is computed.

I then want to create a new vector y2. This vector will contain the first 3 elements of vector y and the last 3 elements of vector y. The elements in between will come from y1.

I am getting an error

??? Error using ==> horzcat
CAT arguments dimensions are not consistent..

but the vectors y and y1 are the same size and direction.

When I type y(1:3), y1(4:end-3), y(end-2:end) separately into the command line I get the elements that I am expecting and when you sum their lengths together it equals the length of y and y1.

Would anybody know what is going wrong?

Thank you

sch_cycle=xlsread('C:\Autonomie practice\RW.xls','Input_data');
nrows = size(sch_cycle,1)-1;
x = sch_cycle(:,1);
y = sch_cycle(:,2);
N= size(sch_cycle,1);
y2 = [y(1:3) y1(4:end-3) y(end-2:end)];




No products are associated with this question.

3 Answers

Answer by Andrei Bobrov
on 23 Mar 2012
 Accepted answer

y = y(:);
y1 = y1(:);
y2 = [y(1:3); y1(4:end-3); y(end-2:end)];


Answer by Jonathan Sullivan on 23 Mar 2012

You'll want to check the size of them. Sounds like one or more of your vectors are column vectors [Nx1]. You want to make sure they are all row vectors {1xN] if you are going to concatenate them horizontally. Either that, or use the vertical concatenation syntax as follows.

[a; b; c]


Answer by Wayne King
on 23 Mar 2012

y is definitely a column vector, is y1 a row vector?


If you do

    y = y';

That should fix the problem.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!