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
Can you give an example or code ?
Rik
2020년 4월 24일
@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);
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
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
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
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에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
