why is my code ignoring my while loop
4 views (last 30 days)
Show older comments
In two cases, the code is ignoring what i put in a while loop and will just run whatever is after it. It will not run what is asked inside the loop.
example 1:
c1 = table2array(cm);
q = quantile(c1, [0.1 0.9])
k1 = table2array(kg);
q2 = quantile(k1, [0.12 0.88]) %decided due to looking at data by eye and determining which were the obvious outliers
figure
hold on
xlim([145 200])
ylim([50 135])
%check outliers
while cm{1:end, 'Fun_height'} < (q(2)) & cm{1:end, 'Fun_height'} > (q(1)) %where an outlier is a value that is less/greater than the 10th/90th quantile
if kg{1:end, 'Fun_weight'} < (q2(2)) & kg{1:end, 'Fun_weight'} > (q2(1)) %where an outlier is a value that is less/greater than the 15th/85th quantile
scatter(cm{1:end, 'Fun_height'}, kg{1:end, 'Fun_weight'},'ro')
end
end
scatter(cm{1:end, 'Fun_height'}, kg{1:end, 'Fun_weight'},'ro')
grid on
ylabel('weight')
xlabel('height')
hold off
here, it just plots data from the scatter after the the while loop, ignoring everything inside the loop.
example 2 :
n = input('value of n')
%input user value for n
x = input('value of x')
%input user value for x
y = input('value of y')
%input user value for y
k = 1
while k< n && k>=1
k = k + 1;
seriessum = @(n,x,y) sum((x./y).^k);
end
seriessum = @(n,x,y) sum((x./y).^k);
s = seriessum(n,x,y);
figure
plot(k,s, 'ro')
Here, it just plots the value as if k =1, ignoring the while loop again.
2 Comments
ME
on 20 Dec 2019
It's hard to give a proper answer here since we don't have access to the cm and kg data.
My guess though is that your while loop never does anything because the conditions listed are never met. If you could upload your data to this question then I'll happily take a look and see if I can figure out a slightly more specific answer.
Answers (1)
Rik
on 20 Dec 2019
You shouldn't use logical arrays as your conditional. It will never do what you think it does. This is what it does:
IF=@(cond) ~isempty(cond) && all(cond(:));
For the second example: it does run all the code. It just overwrites the result on every iteration, just as the mlint is warning you. During the plotting you are using k (which is a scalar) and s (which has the same size as x and y for a scalar k).
See Also
Categories
Find more on Logical 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!