MATLAB Answers

For - While loop output and extracting the value of computed rows from a column

1 view (last 30 days)
Hi all. I'm new in MATLAB and in coding in general. I'm practicing but I can't seem to wrap my head around this.
I have a sample data set named 'pre_assign,mat'. It's a structure with 3 tables having varying sizes.
As an example, here's the first table 'pre_assign(1).r_T'
custNo x_coor y_coor d nearest_dist m G p
______ ______ ______ __ ____________ _ _ ______
2 18 16 30 0 2 1 100
7 14 17 20 4.1231 2 1 4.8507
6 16 9 13 7.2801 2 1 1.7857
1 13 6 15 11.18 2 1 1.3416
3 13 6 15 11.18 2 1 1.3416
My end-goal is to get the values of the 'custNo' column that was included in this for-while loop before it terminates:
cumulative_sum = pre_assign(1).r_T.d(1)
C = 64
for qq = [1:5]
while cumulative_sum <= C
cumulative_sum = cumulative_sum + pre_assign(1).r_T.d(1+qq)
end
end
when I run this code above, it ouputs:
cumulative_sum =
30
C =
64
cumulative_sum =
50
cumulative_sum =
70
My questions are the following:
(1) How come that the end result of cumulative_sum is 70? Shouldn't it stopped at pre_assign(1).r_T.d(3) where the actual cumulative sum of column 'd' at that point is 63? (i.e. 30 + 20 + 13 which is < C = 64)
(2) Is it possible to extract the values of column 'custNo' that was included in the loop before it terminated? (i.e. custNo 2, 7, 6)
Any tips or help is greatly appreciated. Thank you.

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 19 Nov 2020
Christopher - look closely at this code
for qq = [1:5]
while cumulative_sum <= C
cumulative_sum = cumulative_sum + pre_assign(1).r_T.d(1+qq)
end
end
The qq does not change when in the body of the while loop. So you will always add pre_assign(1).r_T.d(1+qq) to the cumulative sum until it exceeds C. I think that you want to replace the while loop with an if condition check. Perhaps something like
for qq = [1:5]
if cumulative_sum + pre_assign(1).r_T.d(1+qq) <= C
cumulative_sum = cumulative_sum + pre_assign(1).r_T.d(1+qq);
else
break;
end
end
To get the custom numbers, just create an array to populate with on each iteration.
custNo = pre_assign(1).r_T.custNo(1);
for qq = [1:5]
if cumulative_sum + pre_assign(1).r_T.d(1+qq) <= C
cumulative_sum = cumulative_sum + pre_assign(1).r_T.d(1+qq);
custNo = [custNo pre_assign(1).r_T.custNo(1+qq)];
else
break;
end
end

  3 Comments

Geoff Hayes
Geoff Hayes on 19 Nov 2020
You could also change how you iterate over the array. Instead of 1:5 which may cause a problem when qq is 5 (and so pre_assign(1).r_T.d(1+qq) may not be valid), just do
custNo = pre_assign(1).r_T.custNo(1);
for qq = 2:length(pre_assign(1).r_T.d)
if cumulative_sum + pre_assign(1).r_T.d(qq) <= C
cumulative_sum = cumulative_sum + pre_assign(1).r_T.d(qq);
custNo = [custNo pre_assign(1).r_T.custNo(qq)];
else
break;
end
end
Christoper Angelo Malayan
Christoper Angelo Malayan on 19 Nov 2020
Thank you very much for this, Geoff. Using if condition check does indeed makes it more easier to understand.
Although, to be honest, it still do not understand why exactly 'the qq does not change when in the body of the while loop', as you have mentioned. I'll try to practice more and read up on this.
Thanks again!
Geoff Hayes
Geoff Hayes on 19 Nov 2020
Christopher - try using the MATLAB debugger to step through the code and see what is happening when using
for qq = [1:5]
while cumulative_sum <= C
cumulative_sum = cumulative_sum + pre_assign(1).r_T.d(1+qq)
end
end
On each iteration of the for loop, we execute the code within the body. In this case, that code is a while loop so we will continue within that code until the while condition is no longer satisfied. At that point, we will exit the while loop and begin the next iteration of the for loop which is for qq equal to 2. Since the cumulative_sum is already greater than C then we move on to the next iteration of the for loop for qq equal to 3. Etc.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!