I want to create a conditional function

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?

 Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 23 de Feb. de 2021
Editada: KALYAN ACHARJYA el 23 de Feb. de 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;

4 comentarios

Deniz Bozdogan
Deniz Bozdogan el 23 de Feb. de 2021
Thank you for your help.
SRADHARAM SWAIN
SRADHARAM SWAIN el 21 de Mzo. de 2023
How to write C_j-s equation in MATLAB
Steven Lord
Steven Lord el 21 de Mzo. de 2023
@SRADHARAM SWAIN This doesn't appear to be closely related to the original topic of this question. Please ask it as a new question (using the Ask link near the top of this page.) When you do, please show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Sameer khan
Sameer khan el 17 de En. de 2024
function [S Q]=testsalary(TT,BS);
BS=BS*12;
Q=[];
S=[];
for k=1:length(TT)
if TT(k)<=30000
Q{k,1}={'Bad'};
S(k,1)=BS;
elseif (TT(k)>30000) & (TT(k)<=40000)
Q{k,1}={'Average'};
S(k,1)=BS;
else
Q{k,1}={'Good'};
S(k,1)=BS+500;
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 23 de Feb. de 2021

Comentada:

el 17 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by