How to create a periodic function?

6 views (last 30 days)
Klaudjo Aliaj
Klaudjo Aliaj on 31 Oct 2020
Commented: Rayan on 19 Jan 2025
Hi everyone,
I have created a function g_3 which looks like:
t=linspace(-2,2);
g_3=[];
for i=1:length(t)
if abs(t(i))<=1
g_3(i)=1-abs(t(i));
else
g_3(i)=0;
end
end
Now I want to do it periodic with periode 4. How can I do it?
  3 Comments
Rayan
Rayan on 19 Jan 2025
function r = compare_numbers(a,b)
if a>b
r ='greater'
elseif a<b
r ='smaller'
else a==b
r ='equal'
end
Rayan
Rayan on 19 Jan 2025
schaltjahre = 1904:4:2000
anzahlJahre = length(schaltjahre)
stunden = 1:24:8760
anzahlStunden = length(stunden)
jahr = 1:364
wochen = reshape(jahr,7,52)'
jahre = 1900:2000
schaltjahre = mod(jahre,4) == 0 & mod(jahre,100) ~= 0 | mod(jahre,400) == 0
schaltjahreidx = jahre(schaltjahre)
eingabe = [10 20 30 40 50 60 70 80 90 100]
a = length(eingabe)
b = a/2
ausgabe = reshape(eingabe,2,b)
eingabe = [10 20 30 40 50 60 70 80 90 100]
jederZweite = eingabe(2:2:end)
jederZweite(end)= NaN
f = 5
t = 0:0.1:10
s = sin(2*pi*f)*t
lastprofile = [1:120]
lp_min = min(lastprofile,[],2)
lp_max = max(lastprofile,[],2 )
lp_diff = lp_max - lp_min
disp(lp_diff)
lastprofile = [1:500]
[lp_min,lp_minIdx] = min(lastprofile)
[lp_max,lp_maxIdx] = max(lastprofile)
lp_diff = lp_max - lp_min
disp(lp_diff)
Lastprofile = [123 4894 213 490 9 4456]
hoch = Lastprofile > 500
niedrig = Lastprofile <= 150
anzahlHoch = nnz (hoch)
anzahlNiedrig = nnz (niedrig)
function win = compare_numbers(a,b)
if a > b
win = 'greater'
elseif a < b
win = 'smaller'
else a == b
win = 'equal'
end
end

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 31 Oct 2020
You can use repmat()
t=linspace(-2,2);
g_3=zeros(size(t)); % pre-allocating memory (efficient code)
for i=1:length(t)
if abs(t(i))<=1
g_3(i)=1-abs(t(i));
else
g_3(i)=0;
end
end
G_3 = repmat(g_3, 1, 10);
T = linspace(-2, 4*10, 100*10);
plot(T, G_3)
  2 Comments
Klaudjo Aliaj
Klaudjo Aliaj on 3 Nov 2020
Thank you very much, that´s exactly what I was looking for :)
Ameer Hamza
Ameer Hamza on 3 Nov 2020
I am glad to be of help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!