How to include vectors in IF statement?
32 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohammed Lamine Mekhalfia
am 8 Jun. 2022
Bearbeitet: Torsten
am 8 Jun. 2022
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 Kommentare
Akzeptierte Antwort
Mohammed Lamine Mekhalfia
am 8 Jun. 2022
1 Kommentar
Torsten
am 8 Jun. 2022
Bearbeitet: Torsten
am 8 Jun. 2022
if the condition should be true for all elements of the vector S:
if abs(S-time) < 0.0001
Press_out=Pressure_In2
else
Press_out=Pressure_In1
end
if the condition should be true for at least one element of the vector S:
if any(abs(S-time) < 0.0001)
Press_out=Pressure_In2
else
Press_out=Pressure_In1
end
if you want to check the condition for all elements of S separately:
Press_out = Pressure_In1*ones(size(S));
idx = abs(S-time) < 0.0001;
Press_out(idx) = Pressure_In2;
Weitere Antworten (1)
Bhargava Hurulagere Sridharamurthy
am 8 Jun. 2022
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,
0 Kommentare
Siehe auch
Kategorien
Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!