Expected a scalar and it should be a scalar

12 visualizaciones (últimos 30 días)
Henrique Furtado
Henrique Furtado el 30 de Mayo de 2019
Editada: Stephen23 el 30 de Mayo de 2019
I don't understand why I get this error when it should actually be a scalar:
I've tried removing the (:), and "if all (delta_Fx) < 0 but it shows the same error.

Respuesta aceptada

Stephen23
Stephen23 el 30 de Mayo de 2019
Editada: Stephen23 el 30 de Mayo de 2019
If delta_Fx is not scalar, then the output of
-delta_Fx > delta_Fx_max
will not be scalar either. You need to correctly handle all non-scalar conditions, not just the first one.
However it is quite likely that your approach using if is incorrect anyway. It appears that you are attempting an element-wise operation (which if does not do without a loop), namely replace values whose magnitude lies outside some value range, and so you probably should be doing something like this:
delta_Fx = max(-delta_Fx_max,min(delta_Fx_max,delta_Fx))

Más respuestas (1)

Image Analyst
Image Analyst el 30 de Mayo de 2019
Delta_Fx is an vector, so do you want to enter the "if" if any one is true, or only if ALL are true?
Use any or all as appropriate, according wo whatever you want. Some possible options depending on exactly what you want:
if any(delta_Fx < 0 & -delta_Fx > delta_Fx_max)
if all(delta_Fx < 0 & -delta_Fx > delta_Fx_max)
if all(delta_Fx < 0) && all(-delta_Fx > delta_Fx_max)
if all(delta_Fx < 0) && any(-delta_Fx > delta_Fx_max)
if any(delta_Fx < 0) && all(-delta_Fx > delta_Fx_max)
if any(delta_Fx < 0) && any(-delta_Fx > delta_Fx_max)
Note how sometimes I used & and sometimes used &&. I used a single & if I want to AND two multi-element vectors, and && if I want to AND two one-element boolean.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by