Hi. I want to know how can i reduce these if statements with two conditions.
I have tried following code but it is very lengthy to use for like 50 values.
X = {'1';'2A';'2B';'3';'4'}'; %Seismic Zone
Z = [0.075, 0.15, 0.2, 0.3, 0.4]'; %Zone factor
zTbl = table(Z,'RowNames',X); %Table of Zone and Zone Factor
x = input('What is seismic zone?: ','s'); %Seismic Zone input
Z = zTbl{upper(x),'Z'} % Z factor assigned
SP=input('What is SP?: ','s')
SP1=upper(SP)
if Z==0.075 & SP1=='SA'
Ca=0.06
elseif Z==0.075 & SP1=='SB'
Ca=0.08
elseif Z==0.15 & SP1=='SA'
Ca=0.16
.
.
end

 채택된 답변

Rik
Rik 2020년 4월 24일

0 개 추천

Create an array with all implemented values of CA. Then you can use array operations to find the index, which will allow you to easily add cases and detect combinations that aren't implemented.

댓글 수: 16

Can you give an example or code ?
Rik
Rik 2020년 4월 24일
You can do something similar to what Walter did in this answer.
@Rik it is not working for my case as i have one string and one value.
Rik
Rik 2020년 4월 25일
편집: Rik 2020년 4월 25일
No, you have at least two strings and 5 values.
%cols are for values of Z, rows are for values of SP1
Ca_database=[...
0.06 0.16 NaN NaN NaN;... %SP1 is SA
0.08 NaN NaN NaN NaN]; %SP1 is SB
%Z=0.075 0.15 0.2 0.3 0.4
Z_list = [0.075, 0.15, 0.2, 0.3, 0.4]';
SP1_list={'SA','SB'};
SP1='SA';
Z=0.15;
Z_ind=ismember(Z_list,Z);
SP1_ind=ismember(SP1_list,SP1);
if ~any(Z_ind) || ~any(SP1_ind)
error('combination not implemented:\nZ=%.3f\nSP1=%s',Z,SP1)
end
Ca=Ca_database(SP1_ind,Z_ind);
Thanks it resolved my problem.
Hi Rik. Can you help me in resolving similar problem?
I want to use this table. i.e for closest distance(CD) and Source Type(ST) i want to extract value of Na.
For example if CD=1 and ST='A' then
Na=1.5
%Part-2
%Na and Nv
CD=[(<2),5,(>10)]
CD1=input('What is closet distance to known source?:')
ST=input('What is seismic source type?: ','s')
ST1=upper(ST)
NaV=[1.5,1.2,1.0;
1.3,1.0,1.0;
1.0,1.0,1.0;]
ST1_list=['A','B','C']
CD_ind=ismember(CD,CD1);
ST1_ind=ismember(ST1_list,ST1);
if ~any(CD_ind) || ~any(ST1_ind)
disp('Wrong inputs')
end
Na=NaV(ST1_ind,CD_ind)
Error which pops up is
"Unexpected MATLAB operator."
Rik
Rik 2020년 4월 26일
Your CD variable is causing the problem. You can't test inequalities with ismember, only equalities. What is your expected number of values in CD if you ever expand it? Will the upper and lower alway be inequalities? And are you allowed to use =< instead of <?
Yes i am allowed to use =< and also above and lower values will always be inequalities.
And one more thing the remaining values between 2 and 10 will be obtained by linear interpolation i.e for 4km.
Rik
Rik 2020년 4월 26일
When I get back to a computer I'll write up some code, but that will take a while.
My idea is to generate an upper bound matrix and a lower bound matrix. Then you can do something like L= data<=upper_bound | data>=lower_bound;
I will wait for your comment and kindly also consider the interpolation thing in your code .
Rik
Rik 2020년 4월 27일
If you need interpolation that completely changes the question. Then you are no longer simply selecting values.
What you need to do now is select a data row with ST1, and then do whatever interpolation you need. You will have CD and the appropriate row of NaV.
Can you first resolve my 1st issue first? Then i will look into interpolation thing.
Just the same as before, but now with a single variable.
ST=input('What is seismic source type?: ','s')
ST1=upper(ST)
NaV=[1.5,1.2,1.0;
1.3,1.0,1.0;
1.0,1.0,1.0;]
ST1_list=['A','B','C']
ST1_ind=ismember(ST1_list,ST1);
if ~any(ST1_ind)
disp('Wrong inputs')
end
Na=NaV(ST1_ind,:);
But i have to also take care of the closest disatance input. Where will i compare it?
Like this?
CD1=input('What is closet distance to known source?:');
if CD1<CD(1),CD1=CD(1);elseif CD1>CD(end),CD1=CD(end);end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Seismology에 대해 자세히 알아보기

질문:

2020년 4월 24일

댓글:

Rik
2020년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by