Why is the If Statement Not Working?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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])
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!