MATLAB Answers

I want to create a conditional function

44 views (last 30 days)
When i try to create the following function with n=[-25:25] the value of x1 shows as x1=-3
if 0<=n<=8
x1=-3
else
x1=0
What can i do to make the function work?

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 23 Feb 2021
Edited: KALYAN ACHARJYA on 23 Feb 2021
You have not completed the Code, If you are beginner Do Google MATLAB Onramp
Read about if Else, For Loop and Logical Indexing MATLAB
Using if else and loop
n=-25:25;
x1=zeros(1,length(n));
for i=1:length(n);
if n(i)>=0 && n(i)<=8
x1(i)=-3;
else
x1(i)=0;
end
end
x1
Without using loop and if else (Recommended)
n=-25:25;
x1=zeros(1,length(n));
x1(n>=0 & n<=8)=-3;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by