How to include vectors in IF statement?

25 visualizzazioni (ultimi 30 giorni)
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 Commenti
Walter Roberson
Walter Roberson il 8 Giu 2022
if (time>(S(f)-0.002))&&(time<(S(f)+0.002))
is that what you mean?

Accedi per commentare.

Risposta accettata

Mohammed Lamine Mekhalfia
@Bhargava Hurulagere Sridharamurthy Thanks for the feedback
what my question mean:
check time if it is equal to a value there in vector S and based on that choose the output at that value
for example at time equal 0.075 go to the vector S and see if this condition is true
The condition (S(f) -0.0001 )< time < (S(f) +0.0001 ). the vector S contains 15 values so it need to check for all of it.
if this is true then pressure output = 1
if not pressure output = 0
  1 Commento
Torsten
Torsten il 8 Giu 2022
Modificato: Torsten il 8 Giu 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;

Accedi per commentare.

Più risposte (1)

Bhargava Hurulagere Sridharamurthy
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,

Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by