How to include vectors in IF statement?
Show older comments
Dear.
in my code below I am trying to plot an output.
This output is related to a vector S so , the output would equal 1 if the value of time t equals one (1) of the value of the vector S else the output = 0
how to integrate this if statement wit the fact that S is a vector
I tried a lot
Instead of the (3) I need to make an index
My code:
clear all
omega=1000 %rotation frequency (rpm)
omega_rad=(omega*2*pi)/60 %rotation frequency (rad/s)
TRot=(360*(pi/180))/omega_rad %Time of one rotation
Holes=15 %Number of holes in Cylinder
Pressure_In1=0 %Pressure Amplitude in Bar
Pressure_In2=1 %Pressure Amplitude in Bar
alpha=360/Holes %angles between holes (degree)
beta=11.20 %angles swept by 1 hole (degree)
alpha_rad=alpha*(pi/180) %angles between holes (rad)
beta_rad=beta*(pi/180) %angles swept by 1 hole (rad)
k=TRot/100 %timestep
h=1
f=1
for N=1:1:Holes
s= TRot*(N*alpha)/360
S(f)=s
f=f+1
end
for time=0:k:TRot
if (time>(S(3)-0.002))&&(time<(S(3)+0.002))
Press_out=Pressure_In2
else
Press_out=Pressure_In1
end
Press(h)=Press_out
t(h)=time
h=h+1
end
plot(t,Press)
2 Comments
Walter Roberson
on 8 Jun 2022
if (time>(S(f)-0.002))&&(time<(S(f)+0.002))
is that what you mean?
Mohammed Lamine Mekhalfia
on 8 Jun 2022
Accepted Answer
More Answers (1)
Bhargava Hurulagere Sridharamurthy
on 8 Jun 2022
0 votes
Hello Mohammed,
I understand that you want to find whether a given value exists in vector or not. If Exists, access the value by returned index and do further calculations.
You can perform this task using the MATLAB function 'ismember'
Refer to the below page for usage and syntax of MATLAB function 'ismember':
I hope that the information provided helps in resolving your query.
Thanks,
Bhargava,
Categories
Find more on Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!