Multiple if statements with two conditions

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

 Respuesta aceptada

Rik
Rik el 24 de Abr. de 2020

0 votos

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 comentarios

Muhammad Imran
Muhammad Imran el 24 de Abr. de 2020
Can you give an example or code ?
Rik
Rik el 24 de Abr. de 2020
You can do something similar to what Walter did in this answer.
Muhammad Imran
Muhammad Imran el 25 de Abr. de 2020
@Rik it is not working for my case as i have one string and one value.
Rik
Rik el 25 de Abr. de 2020
Editada: Rik el 25 de Abr. de 2020
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);
Muhammad Imran
Muhammad Imran el 26 de Abr. de 2020
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)
Muhammad Imran
Muhammad Imran el 26 de Abr. de 2020
Error which pops up is
"Unexpected MATLAB operator."
Rik
Rik el 26 de Abr. de 2020
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 <?
Muhammad Imran
Muhammad Imran el 26 de Abr. de 2020
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 el 26 de Abr. de 2020
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;
Muhammad Imran
Muhammad Imran el 26 de Abr. de 2020
I will wait for your comment and kindly also consider the interpolation thing in your code .
Rik
Rik el 27 de Abr. de 2020
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.
Muhammad Imran
Muhammad Imran el 27 de Abr. de 2020
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,:);
Muhammad Imran
Muhammad Imran el 28 de Abr. de 2020
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Seismology en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Abr. de 2020

Comentada:

Rik
el 28 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by