If statement with multiple conditions

조회 수: 142 (최근 30일)
Jacqueline Rigatto
Jacqueline Rigatto 2020년 11월 13일
댓글: Jacqueline Rigatto 2020년 11월 14일
clear all; clc; close all
Tp=[4 7 11 16];
u= [0.188 0.368 0.628 0.997];
u10= [5.5 9.7 15 21];
ni_a=1.326*10^(-5);
sigma_p=2.*pi.*Tp.^(-1);
w2=((u.^2)./(ni_a.*sigma_p)).^(1.5);
r0=30:10:500;
SSGF = zeros(numel(Tp),numel(r0));
for Tp_Idx=1:length(Tp)
for r0_Idx=1:length(r0)
if Tp==4
if 30<=r0<75
SSGF=(7.84.*10.^(-3)).*r0.^(-1).*r0;
elseif 75<=r0<200
SSGF=4.41.*10.*(r0.^(-3)).*r0;
else
SSGF=(1.41.*10.^(13)).*r0.^(-8).*r0;
end
elseif Tp==7
if 30<=r0<75
SSGF=(7.84.*10.^(-3)).*r0.^(-1).*r0;
elseif 75<=r0<200
SSGF=4.41.*10.*(r0.^(-3)).*r0;
else
SSGF=(1.41.*10.^(13)).*r0.^(-8).*r0;
end
elseif Tp==11
if 30<=r0<75
SSGF=(7.84.*10.^(-3)).*r0.^(-1).*r0;
elseif 75<=r0<200
SSGF=4.41.*10.*(r0.^(-3)).*r0;
else
SSGF=(1.41.*10.^(13)).*r0.^(-8).*r0;
end
else
if 30<=r0<75
SSGF=(7.84.*10.^(-3)).*r0.^(-1).*r0;
elseif 75<=r0<200
SSGF=4.41.*10.*(r0.^(-3)).*r0;
else
SSGF=(1.41.*10.^(13)).*r0.^(-8).*r0;
end
end
end
end
Above is my code and my problem is that I am not able to make a matrix with 4 columns and 48 lines (SSGF). A piece of the table that was supposed to come out is in the image below.
Thanks in advance

답변 (2개)

Walter Roberson
Walter Roberson 2020년 11월 13일
if 30<=r0<75
means the same as
if ((30<=r0)<75)
The first part, 30<=r0, returns 0 (false) or 1 (true). Then you compare that 0 or 1 to 75.
You need to use &&
if 30 <= r0 && r0 < 75
  댓글 수: 1
Jacqueline Rigatto
Jacqueline Rigatto 2020년 11월 14일
Thank you very much Walter Roberson, it helped a lot

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


Alan Stevens
Alan Stevens 2020년 11월 13일
Don't forget the indices:
if Tp(Tp_Idx)==4
if 30<=r0(r0_Idx)<75
SSGF(Tp_Idx,r0_idx)=(7.84.*10.^(-3)).*r0(ro_Idx).^(-1).*r0(r0_Idx);
...etc.
  댓글 수: 1
Jacqueline Rigatto
Jacqueline Rigatto 2020년 11월 14일
Thank you very much Alan Stevens, it helped a lot

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by