Value not multiplying by -1
Mostrar comentarios más antiguos
I have a nested if statement which works as it is supposed to until the last iteration.
Does anyone know why it is not multiplying the last value by -1?
w = x_0 % zero vector
k = 0 % zero value
i=1 % initial value for count
j=-1 % coefficient
for i = 1:n
if Raw_Data(i,2) == Raw_Data (i,3)
fprintf ('ERROR!!!!!)
else if Raw_Data(i,3) == val
w(i,1) = Raw_Data(i,4)
else if Raw_Data(i,2) == val
w(i,1) = j* Raw_Data(i,4)
% Rationalisation Not Working
else w(i,1)= j* Raw_Data(i,4)
end
end
end
i=i+1
end
4 comentarios
Adam
el 7 de Abr. de 2015
Where does 'val' come from?
Jay
el 7 de Abr. de 2015
Adam
el 7 de Abr. de 2015
So since you used breakpoints in the code where exactly is the problem? Is it not going into the relevant else-if statement or is the code in there not executing correctly?
Jay
el 7 de Abr. de 2015
Respuesta aceptada
Más respuestas (2)
Ingrid
el 7 de Abr. de 2015
in a for loop there is no need to increment the i so just leave out
i = i+1
as this is done automatically.
Also do not use the i as this can be confusing when using imaginary numbers, better practice is to use ii or something completly else (for example counter )
It is not clear why there is an
else if Raw_Data(i,2) == val
when you perform the same function regardless of true/false of this expression
1 comentario
Jay
el 7 de Abr. de 2015
Ilham Hardy
el 7 de Abr. de 2015
Editada: Ilham Hardy
el 7 de Abr. de 2015
1 voto
Learn on how to debug your code,
3 comentarios
Jay
el 7 de Abr. de 2015
Ilham Hardy
el 7 de Abr. de 2015
Ok, did you also notice the mlint warning on your code?
e.g.:
There should not be empty char (read: space) between else and if. So, elseif instead of else if .
Jay
el 7 de Abr. de 2015
Categorías
Más información sobre Whos en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!