Cache values from loop (with differing lengths) to save in 1 variable / matrix
Show older comments
I want to calculate the median for time to an event. The 'time to event' is calculated per file in a for loop (so the loop is needed).
Per file there can be multiple 'time to event' values, and the number of values can differ per file.
So, subsequently I want to pool all the time to event values after all files have been processed and to calculate the median for all files.
For example:
file1 = [2;4;6;8]; -> median is 5
file2 = [1;5;10;15]; -> median is 7,5
combined = [2;4;6;8;1;5;10;15]; -> 5,5
for i = 1:length(files)
time_to_event(i) = dataset.time_to_event;
end
median_time_to_event = nanmedian(time_to_event) % use ~NaN
it tells me that the indices of the left side are not compatible with the right side
So, I need a way to combine the generated time to events to in the loop (although they may have different lengths) and then calculate the median.
I was thinking maybe add the time of events of the seperate files in seperate colums and fill the difference in row length with NaN?
But I havent been able to.
Hopefully anybody can help, thanks!
Accepted Answer
More Answers (1)
Walter Roberson
on 29 May 2022
for i = 1:length(files)
dataset = load something from files(i)
time_to_event{i} = dataset.time_to_event(:);
end
individual_medians = cellfun(@nanmedian, time_to_event);
median_time_to_event = nanmedian(individual_medians);
1 Comment
SRRellum
on 29 May 2022
Categories
Find more on MATLAB 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!