How find the best step for the array.

Hello,
I have following array:
first_tt= 1;step = 7;last_tt = 27;
tt= first_tt:step:last_tt;
Answer:
tt= [1,8,15,22];
So what I want is to include the last number 27, by slightly changing the step, BUT still meet the second array value of original array, in this case 1+7= 8. tt= [1, 8,15,22];
Currently done:
first_tt= 1; step = 7;last_tt = 27; tt= first_tt:step:last_tt; new_step = (last_tt-first_tt)/length(tt); new_tt= first_tt:new_step:last_tt;
Answer:
new_tt = [1,7.5,14,20.5,27];
So what I need is to include second array value of original array, i.e 8, so I'm wondering, if there are any ways of doing it?
new_tt = [1,..., *8 (?)*,....,27];
Even +/-2% would be ok.
i.e
new_tt = [1,..., *7.84 (?)*,....,26.46];
Best Regards,
Ivan

3 件のコメント

Ced
Ced 2016 年 3 月 16 日
編集済み: Ced 2016 年 3 月 17 日
EDIT moved to answer section.
Ivan Shorokhov
Ivan Shorokhov 2016 年 3 月 17 日
編集済み: Ivan Shorokhov 2016 年 3 月 17 日
@Ced, Great, thank you, could you please add your comment in the answer section, so I can select it as the best one.
Ced
Ced 2016 年 3 月 17 日
Glad it helped. Done, thanks!

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

 採用された回答

Ced
Ced 2016 年 3 月 17 日
編集済み: Ced 2016 年 3 月 17 日

0 投票

You need to decide whether you want equidistant steps, or matching numbers.
I'm sure I'm missing something, but if you just want the second and last, then
tt = first_tt:step:last_tt;
if ( tt(end) < last_tt )
tt(end+1) = last_tt;
end
Or, if you only need to match the two numbers, then:
first_tt= 1;step = 7;last_tt = 27;
second_tt = first_tt + step;
n_elements = floor((last_tt-second_tt)/step)+1;
tt = linspace(second_tt,last_tt,n_elements);
The point is, there are a million ways of doing this, but none of them will manage to go from one number to another in equidistant steps without some other compromise.

その他の回答 (0 件)

カテゴリ

製品

質問済み:

2016 年 3 月 16 日

コメント済み:

Ced
2016 年 3 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by