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
採用された回答
Rik
2020 年 4 月 24 日
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 件のコメント
Muhammad Imran
2020 年 4 月 24 日
Can you give an example or code ?
Rik
2020 年 4 月 24 日
Muhammad Imran
2020 年 4 月 25 日
@Rik it is not working for my case as i have one string and one value.
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
2020 年 4 月 26 日
Thanks it resolved my problem.
Muhammad Imran
2020 年 4 月 26 日
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
2020 年 4 月 26 日
Error which pops up is
"Unexpected MATLAB operator."
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 <?
Muhammad Imran
2020 年 4 月 26 日
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
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;
Muhammad Imran
2020 年 4 月 26 日
I will wait for your comment and kindly also consider the interpolation thing in your code .
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.
Muhammad Imran
2020 年 4 月 27 日
Can you first resolve my 1st issue first? Then i will look into interpolation thing.
Rik
2020 年 4 月 27 日
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
2020 年 4 月 28 日
But i have to also take care of the closest disatance input. Where will i compare it?
Rik
2020 年 4 月 28 日
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 についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
