I want to create the following function bu i get an error

조회 수: 2 (최근 30일)
Deniz Bozdogan
Deniz Bozdogan 2021년 7월 12일
댓글: Walter Roberson 2021년 7월 12일
a = randi([2,5]);
Ts = 1/20/a;
dur = 10;
t = -dur/2:Ts:dur/2-Ts;
g = zeros(1,length(t));
g(t>=0 & t<1) = 5*t-2;
g(t<2 & t>=1) = 3;
g(t<4 & t>=2) = -2*t+1;
gives the error Unable to perform assignment because the left and right sides have a different number of elements.What should i do?

채택된 답변

Walter Roberson
Walter Roberson 2021년 7월 12일
a = randi([2,5]);
Ts = 1/20/a;
dur = 10;
t = -dur/2:Ts:dur/2-Ts;
g = zeros(1,length(t));
mask = t>=0 & t<1;
g(mask) = 5*t(mask)-2;
mask = t<2 & t>=1;
g(mask) = 3;
mask = t<4 & t>=2;
g(mask) = -2*t(mask)+1;
g
g = 1×1000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

추가 답변 (1개)

Jogesh Kumar Mukala
Jogesh Kumar Mukala 2021년 7월 12일
Hi,
Assuming you are trying to create a piecewise function g(t), I understand that the problem is with the last three lines of the code you have attached. You can better use symbolic variables to create your piecewise function. Please find the reference to use symbolic variables and create piecewise function.
There are a few examples attached on the doc page that might meet your problem requirements.
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 7월 12일
piecewise applies only to symbolic inputs, and since T is numeric you would need to either create a symbolic function or else subs() the numeric value into a symbolic expression.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by