Filter löschen
Filter löschen

Why is the If Statement Not Working?

1 Ansicht (letzte 30 Tage)
Scott Banks
Scott Banks am 23 Aug. 2023
Verschoben: Voss am 23 Aug. 2023
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 23 Aug. 2023
Bearbeitet: Stephen23 am 23 Aug. 2023
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 Kommentar
Scott Banks
Scott Banks am 23 Aug. 2023
Verschoben: Voss am 23 Aug. 2023
Brilliant, Stephen, Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by