Help aligning two vectors with one centred on zero.

I have a set of x values:
x1=-31, -15.5, 0, 14.9,30.2
I know that the separation should be 15 (=pitch) so I want to plot x1 against xreal then do a fit and look at the largest deviation.
I can create:
xreal=Pitch*((1:numel(x1)));
But I can't figure out how to align them AROUND 0,e.g
xreal = -30, -15, 0 , 15, 30
then I can easily plot x1 against xreal and linearly fit.

 採用された回答

LauraLee Austin
LauraLee Austin 2016 年 10 月 25 日

0 投票

How about this, it should allow the location of 0 in x1 to change and the number of values in set x1 to be variable as well. if true
x1 = [-31, -15.5, 0, 14.9,30.2];
Pitch = 15;
idx = find(x1==0);
numel(x1)
xreal=(Pitch*(1-idx)):Pitch:(Pitch*(numel(x1)-idx));

その他の回答 (2 件)

Chaya N
Chaya N 2016 年 10 月 25 日
編集済み: Chaya N 2016 年 10 月 25 日

1 投票

xreal=Pitch*((1:numel(x1)) - ceil(numel(x1)/2))

2 件のコメント

Jason
Jason 2016 年 10 月 25 日
doesn't this assume that the x1 values are symmetrical around 0. although shown in the example, this may not always be the case.
Chaya N
Chaya N 2016 年 10 月 25 日
"...align them AROUND 0"
Exactly as asked!

サインインしてコメントする。

Matt J
Matt J 2016 年 10 月 25 日

0 投票

xreal=xreal-mean(xreal);

3 件のコメント

Jason
Jason 2016 年 10 月 25 日
what if the numbers weren't so symmetrical. i.e. x1=-31, -15.5, 0, 14.9,30.2, 45
Thanks
Matt J
Matt J 2016 年 10 月 25 日
編集済み: Matt J 2016 年 10 月 25 日
The mean of xreal would still be at zero. That is the only interpretation of the requirement "centered AROUND 0" that we can intuit from your post.
Jason
Jason 2016 年 10 月 25 日
Yes sorry, I only thought of it after you had answered.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2016 年 10 月 25 日

コメント済み:

2016 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by