Multiple if elseif statement

28 ビュー (過去 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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by