why is my code ignoring my while loop

5 views (last 30 days)
kavita mudan
kavita mudan on 20 Dec 2019
Commented: kavita mudan on 20 Dec 2019
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
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.
kavita mudan
kavita mudan on 20 Dec 2019
There is a lot of data so i will just show you the begining of it
kg =
Fun_weight
__________
118.04
83.99
77.18
79.45
76.272
453.09
78.088
77.18
66.738
67.192
63.56
77.18
59.02
93.07
83.99
63.56
70.37
59.02
72.64
59.02
74.91
131.66
56.75
452.18
79.45
47.67
64.468
72.64
97.61
72.64
59.02
93.07
83.99
102.15
94.886
106.69
63.56
102.15
81.72
62.652
90.8
106.69
77.18
97.61
61.29
56.75
65.83
77.18
93.07
52.21
79.45
68.1
81.72
54.48
452.18
65.83
87.168
72.64
59.02
63.56
77.18
108.96
99.88
49.94
79.45
79.45
71.732
81.72
53.572
87.622
85.806
59.02
74.91
54.48
63.56
65.83
90.8
59.02
78.996
91.708
77.18
68.1
86.26
90.8
60.836
61.29
452.18
and cm =
Fun_height
__________
187.96
177.8
154.94
172.72
167.64
248.92
251.46
177.8
165.1
162.56
165.1
167.64
251.46
154.94
154.94
165.1
172.72
162.56
162.56
167.64
170.18
182.88
154.94
243.84
165.1
154.94
167.64
162.56
182.88
162.56
162.56
180.34
162.56
182.88
177.8
167.64
187.96
193.04
172.72
167.64
167.64
175.26
154.94
162.56
167.64
175.26
167.64
180.34
180.34
160.02
172.72
190.5
162.56
157.48
243.84
165.1
162.56
152.4
157.48
154.94
160.02
182.88
182.88
160.02
170.18
165.1
162.56
154.94
167.64
165.1
172.72
162.56
180.34
165.1
160.02
162.56
182.88
165.1
180.34
182.88
162.56
165.1
182.88
175.26
167.64
162.56
243.84
I have checked and the conditions should only contain non-zero values.
Thanks so much for taking a look

Sign in to comment.

Answers (1)

Rik
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).
  1 Comment
kavita mudan
kavita mudan on 20 Dec 2019
Thanks, I have figured this part out and it was just that the data was being overwritten

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!