For - While loop output and extracting the value of computed rows from a column
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Christoper Angelo Malayan
el 19 de Nov. de 2020
Comentada: Geoff Hayes
el 19 de Nov. de 2020
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 comentarios
Respuesta aceptada
Geoff Hayes
el 19 de Nov. de 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 comentarios
Geoff Hayes
el 19 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!