Cache values from loop (with differing lengths) to save in 1 variable / matrix
조회 수: 4 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
Voss
2022년 5월 29일
To handle vectors of different lengths, you can make time_to_event a cell array:
time_to_event = cell(numel(files),1);
for ii = 1:numel(files)
% dateset = data from the ii-th file
time_to_event{ii} = dataset.time_to_event;
end
Then the median of all elements of all time_to_event vectors can be found like this (assuming each one is a column vector like you show in your examples):
median_time_to_event = nanmedian(vertcat(time_to_event{:}));
And the median of each element of time_to_event (i.e., each time-to-event vector) can be found like this:
median_each = cellfun(@nanmedian,time_to_event)
Here's running a concrete case not based on any data:
time_to_event = cell(4,1);
for ii = 1:4
time_to_event{ii} = rand(randi(10),1);
end
disp(time_to_event)
median_time_to_event = nanmedian(vertcat(time_to_event{:}))
median_each = cellfun(@nanmedian,time_to_event)
추가 답변 (1개)
Walter Roberson
2022년 5월 29일
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!