Multiple if elseif statement

조회 수: 26 (최근 30일)
rakesh kumar
rakesh kumar 2021년 4월 5일
댓글: rakesh kumar 2021년 4월 5일
How to write multiple if else if statement . Is there any vectoristion method for it. I want to write the statement five hundred times.
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 4월 5일
Can you share representative problems and expected results?
KSSV
KSSV 2021년 4월 5일
if else can be replaced by using indexing most of the time. Read about it.

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

채택된 답변

Steven Lord
Steven Lord 2021년 4월 5일
It depends on what you're trying to do. If you want to do something like:
  • If x is between 0 and 1, in the range [0, 1), let y be some value
  • If x is between 1 and 2, in the range [1, 2), let y be a different value
  • If x is between 2 and 3, in the range [2, 3), let y be a third value
  • etc
consider using the discretize function or use a for loop over the intervals or use findgroups and splitapply or use groupsummary or ...
If you give us more detailed information about what you're trying to do we may be able to offer more specific suggestions.
  댓글 수: 1
rakesh kumar
rakesh kumar 2021년 4월 5일
Dear Steven in the given code I want to have the values of of x for different values of i.Here the valu of i is from 1 to 2 but in actual poblem the value of i can go to 500. I have to calculate the mean value of last column of x for different values of i. A=randi(2,2); b=A*transpose(A); c=chol(b,'lower'); p=normrnd(0,1,[1,10]); q=p(p>0); z=q(1:2); E1=c*transpose(z); E=transpose (E1);
K=[67.3061 -8.1429;-8.1429 59.1633] b=[0;4]; x(:,1)=[0;0] r(:,1)=b d(:,2)=r(:,1) for i=1:length(E)
i a(:,:,i)=E(i)*K
for k=1:20 p1=transpose(d(:,k+1))*a(:,:,i)*d(:,k+1) q1=(transpose(r(:,k))*r(:,k));
s(k+1)=q1/p1
x(:,k+1)=x(:,k)+s(k+1).*d(:,k+1)
if i== 1
m1 = x
elseif i == 2
m2=x
M=[m1 m2] end
u=a(:,:,i)*d(:,k+1) v=s(k+1).*u
r(:,k+1)=r(:,k)-v
if r(:,k+1)<=0.001
break;
end
w=transpose(r(:,k+1))*r(:,k+1);
w1=transpose(r(:,k))*r(:,k);
c(:,k+1)=w/w1;
l=d(:,k+1)
g=(w/w1)*l
d(:,k+2)=r(:,k+1)+g
k=k+1;
end
end

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

추가 답변 (1개)

Sajid Afaque
Sajid Afaque 2021년 4월 5일
you can try switch statements as alternative for else if.
depends on your need

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by