Issue with multiple If condition

2 次查看(过去 30 天)
Hello, I have an issue with the if condition.
I have the following values for the case I have the issue
P_matrix=[53.9350, 53.95, 54125];
S_matrix=[-1, 60.485, 60.57];
TS_arrival_lag=5.3257;
TS_checking=0.9*TS_arrival_lag;
This is the code. The part that is bolted was added to identify a specific case with another condition to explore more options in the selection of the results. However, when it goes inside the condition it gets out from the entire condition despite the condition is met. I added break points to find out the issue and it works for the line indicated below which the target result when the condition is met. I would appreciate the help.
%% Analysis of the P- and S-wave arrivals
%Sorted P-wave Matrix
P_matrix=[plocx; plocy; plocz];
P_matrix=sort(P_matrix);
% Sorted S-wave Matrix
S_matrix=[slocx; slocy; slocz];
S_matrix=sort(S_matrix);
% Estimating the variance between whole P-wave matrix and the two consecutive maximum values
Var_P_wave1=var(P_matrix);
Var_P_wave2=var([P_matrix(1,1);P_matrix(2,1)]);
Var_P_wave3=var([P_matrix(2,1);P_matrix(3,1)]);
% Estimating the variance between the whole S-wave matrix and the two consecutive maximum values
Var_S_wave1=var(S_matrix);
Var_S_wave2=var([S_matrix(2,1);S_matrix(3,1)]);
Var_S_wave3=var([S_matrix(1,1);S_matrix(2,1)]);
%% Estimation of the P-wave and S-wave for windowing procedure
if Var_P_wave1<1.5 && Var_S_wave1<1.5
Pt=mean(P_matrix);
St=mean(S_matrix);
elseif Var_P_wave1<1.5 && Var_S_wave1>1.5
if Var_S_wave2<1.5 && mean([S_matrix(2,1);S_matrix(3,1)])>=(mean(P_matrix)+0.5*TS_arrival_lag) && mean([S_matrix(2,1);S_matrix(3,1)])<=(mean(P_matrix)+1.5*TS_arrival_lag)
Pt=mean(P_matrix); ____________ (This works by using break points which it the condition met)
St=mean([S_matrix(2,1);S_matrix(3,1)]); _____________(This works by using break points which is the condition met)
elseif Var_S_wave3<1.5 && mean([S_matrix(1,1);S_matrix(2,1)])>=(mean(P_matrix)+0.5*TS_arrival_lag) && mean([S_matrix(1,1);S_matrix(2,1)])<=(mean(P_matrix)+1.5*TS_arrival_lag)
Pt=mean(P_matrix);
St=mean([S_matrix(1,1);S_matrix(2,1)]);
else
Pt=mean(P_matrix);
St=Pt+ TS_checking;
end
elseif Var_P_wave2<1.5 && Var_S_wave2<1.5
Pt=mean([P_matrix(1,1);P_matrix(2,1)]);
St=mean([S_matrix(2,1);S_matrix(3,1)]);
elseif Var_P_wave3<1.5 && Var_S_wave2<1.5
Pt=mean([P_matrix(2,1);P_matrix(3,1)]);
St=mean([S_matrix(2,1);S_matrix(3,1)]);
elseif Var_P_wave2<1.5 && Var_S_wave3<1.5
Pt=mean([P_matrix(1,1);P_matrix(2,1)]);
St=mean([S_matrix(1,1);S_matrix(2,1)]);
elseif Var_P_wave3<1.5 && Var_S_wave3<1.5
Pt=mean([P_matrix(2,1);P_matrix(3,1)]);
St=mean([S_matrix(1,1);S_matrix(2,1)]);
else
Pt=min(P_matrix);
St=max(S_matrix);
end
  2 个评论
Torsten
Torsten 2023-8-18
We cannot check your claim because you didn't include executable code.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2023-8-18
编辑:Cris LaPierre 2023-8-18
In the example you have shared, var(P_matrix) is not less than 1.5, so the bolded code in the nested if statement is not reached. The condition that is true is Var_P_wave2<1.5 && Var_S_wave2<1.5
P_matrix=[53.9350; 53.95; 54125];
S_matrix=[-1; 60.485; 60.57];
Var_P_wave1=var(P_matrix)
Var_P_wave1 = 9.7456e+08
Var_P_wave2=var([P_matrix(1,1);P_matrix(2,1)])
Var_P_wave2 = 1.1250e-04
Var_P_wave3=var([P_matrix(2,1);P_matrix(3,1)])
Var_P_wave3 = 1.4618e+09
Var_S_wave1=var(S_matrix)
Var_S_wave1 = 1.2619e+03
Var_S_wave2=var([S_matrix(2,1);S_matrix(3,1)])
Var_S_wave2 = 0.0036
Var_S_wave3=var([S_matrix(1,1);S_matrix(2,1)])
Var_S_wave3 = 1.8902e+03
if Var_P_wave1<1.5 && Var_S_wave1<1.5
disp("1")
elseif Var_P_wave1<1.5 && Var_S_wave1>1.5
disp("2")
if Var_S_wave2<1.5 && mean([S_matrix(2,1);S_matrix(3,1)])>=(mean(P_matrix)+0.5*TS_arrival_lag) && mean([S_matrix(2,1);S_matrix(3,1)])<=(mean(P_matrix)+1.5*TS_arrival_lag)
disp("3")
elseif Var_S_wave3<1.5 && mean([S_matrix(1,1);S_matrix(2,1)])>=(mean(P_matrix)+0.5*TS_arrival_lag) && mean([S_matrix(1,1);S_matrix(2,1)])<=(mean(P_matrix)+1.5*TS_arrival_lag)
disp("4")
else
disp("5")
end
elseif Var_P_wave2<1.5 && Var_S_wave2<1.5
disp("6")
elseif Var_P_wave3<1.5 && Var_S_wave2<1.5
disp("7")
elseif Var_P_wave2<1.5 && Var_S_wave3<1.5
disp("8")
elseif Var_P_wave3<1.5 && Var_S_wave3<1.5
disp("9")
else
disp("10")
end
6

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by