basic code question

Hello,
If I had two vectors x1 and x2. How could I make vector x3 with all the elements of x2 except the first 3 elements and last 3 elements are from x1?
Many thanks
John

1 件のコメント

Oleg Komarov
Oleg Komarov 2012 年 3 月 22 日
Please give a specific title to your questions. Otherwise it's hard to tell wether you're double-posting or not.

回答 (3 件)

Thomas
Thomas 2012 年 3 月 22 日

1 投票

Is this what you wanted?
x1=1:10
x2=11:20
y=[x2(3:length(x2)) x1(1:length(x1-3))]

5 件のコメント

John
John 2012 年 3 月 22 日
Hi,
I meant,
x1=1:10
x2=11:20
then x3 = 1,2,3,14,15,16,17,8,9,10
Thomas
Thomas 2012 年 3 月 22 日
y=[x1(1:3) x2(4:7) x1(8:10)]
Wayne King
Wayne King 2012 年 3 月 22 日
x3 = [x1(1:3) x2(4:end-3) x1(end-2:end)];
John
John 2012 年 3 月 22 日
Thanks,
I'm trying to use it in this context but I'm getting an error for y2
Error using ==> horzcat
CAT arguments dimensions are not consistent.
but y1 and y2 are the same length and direction?
Thank you
for k = 1:length(files)
sch_cycle = xlsread(files{k}, 'Input_data');
nrows = size(sch_cycle,1)-1;
x = sch_cycle(:,1);
y = sch_cycle(:,2);
h=3;
N= size(sch_cycle,1);
r=ksr(x,y,h,N)
y1=r.f';
y2 = [y(1:3) y1(4:end-3) y(end-2:end)];
Oleg Komarov
Oleg Komarov 2012 年 3 月 23 日
Concatenate vertically as I show in my example.
Oleg Komarov
Oleg Komarov 2012 年 3 月 22 日

1 投票

If same size:
x1(4:end-2) = x2(4:end-2);
Otherwise:
[x1(1:3); x2(4:end-2); x1(end-2:end)]
Geoff
Geoff 2012 年 3 月 22 日

0 投票

To get all but the last 3, use this:
x1(1:end-3)
You can work out how to get all but the first 3 =)
And you already know how to concatenate vectors.

1 件のコメント

Geoff
Geoff 2012 年 3 月 22 日
Oops, I misread. You want to get the last three only:
x1(end-2:end)

この質問は閉じられています。

タグ

質問済み:

2012 年 3 月 22 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by