Effacer les filtres
Effacer les filtres

How do I save the output from a double for loop to a vector?

6 vues (au cours des 30 derniers jours)
matt beemsterboer
matt beemsterboer le 25 Avr 2023
Commenté : dpb le 26 Avr 2023
Hi all,
I am trying to use a double for loop to count the number of patients admitted to a hospital (from a data set in excel) during each of the 24 hours in a day. I believe that the for loops are correct but I am unsure as to how I save the outputs (number of patients admitted each hour) in a vector?
counter = 0;
for n = 0:23
for i = 1:height(Exceldata)
if (duration(n,0,0) <= NUM_Time_Data(i)) & (NUM_Time_Data(i) <= duration(n,59,0))
counter = counter + 1;
end
end
VectorOfValues(n) = counter;
end

Réponse acceptée

dpb
dpb le 25 Avr 2023
counter = 0;
VectorOfValues=zeros(24,1); % preallocate
for n = 0:23
for i = 1:height(Exceldata)
if (duration(n,0,0) <= NUM_Time_Data(i)) & (NUM_Time_Data(i) <= duration(n,59,0))
counter = counter + 1;
end
end
VectorOfValues(n+1) = counter;
end
But, "the MATLAB way" to do this will eliminate both loops -- use histcounts or, better yet, put the Excel data into a Matlab table and use groupsummary with the time data turned into datetime or timeofday class. See the general doc on tables and processing by grouping variables; it'll be a big timesaver in the long run compared to going at it brute force for every little detail.
  2 commentaires
matt beemsterboer
matt beemsterboer le 26 Avr 2023
For my purposes, the counter must be within the first loop, not outside both of them. But I forgot that I had to preallocate an array of a certain size. Thank you for that.
dpb
dpb le 26 Avr 2023
Still, it would be far better to load the data into a more useful form for analyses...
tPatients=table(datetime(2020,randi([1 12],1000,1),randi([0 23],1000,1)), ...
hours(randi([0 23],1000,1))+minutes(randi([0 59],1000,1)), ...
'VariableNames',{'AdmittingDate','AdmittingTime'});
tPatients=sortrows(tPatients,{'AdmittingDate','AdmittingTime'});
tPatients.AdmittingTime.Format='hh:mm';
head(tPatients)
AdmittingDate AdmittingTime _____________ _____________ 31-Dec-2019 00:35 31-Dec-2019 01:49 31-Dec-2019 02:32 01-Jan-2020 14:59 01-Jan-2020 19:45 02-Jan-2020 01:29 02-Jan-2020 10:09 02-Jan-2020 10:59
groupcounts(tPatients,'AdmittingTime','hour')
ans = 24×3 table
hour_AdmittingTime GroupCount Percent __________________ __________ _______ [00:00, 01:00) 33 3.3 [01:00, 02:00) 47 4.7 [02:00, 03:00) 50 5 [03:00, 04:00) 43 4.3 [04:00, 05:00) 43 4.3 [05:00, 06:00) 47 4.7 [06:00, 07:00) 26 2.6 [07:00, 08:00) 39 3.9 [08:00, 09:00) 49 4.9 [09:00, 10:00) 40 4 [10:00, 11:00) 56 5.6 [11:00, 12:00) 33 3.3 [12:00, 13:00) 43 4.3 [13:00, 14:00) 46 4.6 [14:00, 15:00) 33 3.3 [15:00, 16:00) 53 5.3

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by