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 CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by