Multiple if elseif statement

7 次查看(过去 30 天)
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