Expected a scalar and it should be a scalar

12 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2019-5-30
编辑:Stephen23 2019-5-30
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))

更多回答(1 个)

Image Analyst
Image Analyst 2019-5-30
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.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by