Why is the If Statement Not Working?

2 ビュー (過去 30 日間)
Scott Banks
Scott Banks 2023 年 8 月 23 日
移動済み: Voss 2023 年 8 月 23 日
Hi guys,
I am trying to create a simple If Statement within a for loop, but I am not getting the correct results.
I am running a loop 6 times, and for i = 1 and i = 4 I want a different computation. Here is the code
Kc = [1 0 0 -1 0 0
0 12 6*L 0 -12 6*L
0 6*L 4*L^2 0 -6*L 2*L^2
-1 0 0 1 0 0
0 -12 -6*L 0 12 -6*L
0 6*L 2*L^2 0 -6*L 4*L^2]
for i = 1:6
if i 1 | 4
Kc(i,:) = Kc(i,:)*(E*A)/L
else
Kc(i,:) = Kc(i,:)*(E*I)/L^3
end
end
The above code does everything for
Kc(i,:) = Kc(i,:)*(E*A)/L
and not i = 2,3,5,6 for
Kc(i,:) = Kc(i,:)*(E*I)/L^3
Can someone explain and help please?
Many Thanks.

採用された回答

Stephen23
Stephen23 2023 年 8 月 23 日
編集済み: Stephen23 2023 年 8 月 23 日
This code
if i 1 | 4
is exactly equivalent to writing
if i
1 | 4
where the second line is calculated and the result immediately discarded, i.e. it does nothing useful at all.
You probably intended this:
if i==1 || i==4
or this:
if any(i==[1,4])
or this:
if ismember(i,[1,4])
  1 件のコメント
Scott Banks
Scott Banks 2023 年 8 月 23 日
移動済み: Voss 2023 年 8 月 23 日
Brilliant, Stephen, Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by