Here I was trying find out various values of created functions for values of t. But there seems to be an error please help.
The error,
"Array indices must be positive integers or logical values.
Error in nana>myfunc1 (line 20)
er = e(i);
Error in nana (line 6)
a = myfunc1(fs*t + 1);"
My code:
f = 200;
fs = 100*f;
t = [0:1/fs:40];
a = myfunc1(fs*t + 1);
b = myfunc2(fs*t + 1);
disp(a);
disp(b);
plot(t,a,t,b)
function er = myfunc1(i)
f=200; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
time=0:1/fs:40; % time vector
e=zeros(size(time));
e(1:fs/f:end)=1;
er = e(i);
end
function edotr = myfunc2(j)
f=200; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
time=0:1/fs:40; % time vector
e=zeros(size(time));
e(1:fs/f:end)=1;
edot = diff(e);
eedot = length(time);
eedot = edot;
eedot(length(time)) = edot(length(time) - 1);
edotr = eedot(j) ;
end

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 2월 13일

0 개 추천

You have an indexing problem. You input to myfunc1 is used to index e. Your calculate it as fs*t + 1. The result is a 1x800001 vector of numbers, and only every 200th number is an integer. You can't use decimal numbers to index an array.
e=1:5;
% Correct
e(2)
ans = 2
% Incorrect
e(2.3)
Array indices must be positive integers or logical values.

카테고리

제품

릴리스

R2020b

질문:

2021년 2월 13일

답변:

2021년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by