Creating a function similar to a sawtooth function

3 ビュー (過去 30 日間)
John Carroll
John Carroll 2022 年 10 月 26 日
コメント済み: John Carroll 2022 年 10 月 27 日
Hello
I am trying to create a function similar to the sawtooth function. I would like a simple set of commands that would create a 1xn matrix of values based on the input of a maximum number and step size. For example I'd like to give the input of 20 for the maximum and a step size of 5 and receiver a matrix of the following form.
[0 5 10 15 20 15 10 5 0 -5 -10 -15 -20 -15 -10 -5 0]
The sawtooth function seems to create something similar but I was hoping someone could seggest another way that could produce the matrix as I am showing.
Thank you

採用された回答

Voss
Voss 2022 年 10 月 26 日
n = 17;
max_val = 20;
step_val = 5;
m = max_val/step_val;
temp = step_val*[0:m m-1:-1:-m -m+1:-1];
result = repmat(temp,1,ceil(n/numel(temp)));
result = result(1:n)
result = 1×17
0 5 10 15 20 15 10 5 0 -5 -10 -15 -20 -15 -10 -5 0
  4 件のコメント
Les Beckham
Les Beckham 2022 年 10 月 27 日
FWIW, here is another approach where you specify the number of cycles instead of the number of data points.
n = 5; % number of cycles
max_val = 20;
step_val = 5;
temp = 0:step_val:max_val;
result = [repmat([temp(1:end-1) flip(temp(2:end))], 1, n) 0];
plot(result)
grid on
John Carroll
John Carroll 2022 年 10 月 27 日
I'm going to try this one as well and see if it rans any better in my code. Thanks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by