6 views (last 30 days)

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.

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).

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/497559-why-is-my-code-ignoring-my-while-loop#comment_779845

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/497559-why-is-my-code-ignoring-my-while-loop#comment_779845

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/497559-why-is-my-code-ignoring-my-while-loop#comment_779847

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/497559-why-is-my-code-ignoring-my-while-loop#comment_779847

Sign in to comment.