Why is the If Statement Not Working?

2 visualizaciones (últimos 30 días)
Scott Banks
Scott Banks el 23 de Ag. de 2023
Movida: Voss el 23 de Ag. de 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.

Respuesta aceptada

Stephen23
Stephen23 el 23 de Ag. de 2023
Editada: Stephen23 el 23 de Ag. de 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 comentario
Scott Banks
Scott Banks el 23 de Ag. de 2023
Movida: Voss el 23 de Ag. de 2023
Brilliant, Stephen, Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by