How to include vectors in IF statement?
조회 수: 21 (최근 30일)
이전 댓글 표시
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
채택된 답변
Mohammed Lamine Mekhalfia
2022년 6월 8일
댓글 수: 1
Torsten
2022년 6월 8일
편집: Torsten
2022년 6월 8일
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;
추가 답변 (1개)
Bhargava Hurulagere Sridharamurthy
2022년 6월 8일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!