why it does not go to if statement and not only in else?(variable 'omega' might be set by a nonscalar operator)
1 view (last 30 days)
Show older comments
Abdulmuti Ehsan on 15 Mar 2022
Commented: Geoff Hayes on 15 Mar 2022
Longitude=39.1925; % East
for d=[21 52 80 111 141 172 202 233 264 294 325 355]
ylabel('Solar Elevation (Altitude)','fontsize',14)
title('Sun Path Chart','fontsize',14)
fh = figure(1);
set(fh, 'color', 'white');
@Abdulmuti Ehsan omega is an array
. So for the condition
what do you mean this to be? Should all elements in omega be greater than zero (which won't be the case) or do you want to be iterating over omega (somehow)?
Steven Lord on 15 Mar 2022
From the documentation page for the if keyword: "if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false."
That means if your expression results in a non-scalar value the body of the if statement is only executed if the expression results in a non-empty result and all the elements in that result are true. In your case, with if omega > 0, if any of the values of omega are not strictly greater than 0, the if statement is not satisfied.
What are your omega values? Are any of them not greater than 0?
Yes, there are several that are not greater than 0.
You could iterate over the elements in omega or you could use logical indexing to operate first on those values of omega that are positive and then those that aren't.
y = NaN(size(omega));
positive = omega > 0;
y(positive) = omega(positive).^2;
negative = omega < 0;
y(negative) = sind(omega(negative));
y.' % Transpose to make it easier to see more of y at once
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!