Multiple if elseif statement

17 views (last 30 days)
rakesh kumar
rakesh kumar on 5 Apr 2021
Commented: rakesh kumar on 5 Apr 2021
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 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 5 Apr 2021
Can you share representative problems and expected results?
KSSV
KSSV on 5 Apr 2021
if else can be replaced by using indexing most of the time. Read about it.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 5 Apr 2021
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 Comment
rakesh kumar
rakesh kumar on 5 Apr 2021
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

Sign in to comment.

More Answers (1)

Sajid Afaque
Sajid Afaque on 5 Apr 2021
you can try switch statements as alternative for else if.
depends on your need

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!