Stop loop when first result is hit / loop of comparison bewteen different tbl heights?

1 visualización (últimos 30 días)
My question can be formulated in 2 ways because I'm not sure about the strategy to use.
I'm a beginner coder, and have to make a comparison between two pieces of tables (output of a bigger code) that have different heights. I need to see if the start and stop days reported are overlapping between these tables. Those are days of assumption of two different drugs on a single patient, I need to see when they were assumed in the same days, but of course neither was given for the same amount of time, therefore tables have a different number of rows.
My idea was to report in the results table a 1 when there was one overlap in the days, and 0 if there's none. I don't care how many times they overlap, I only want to see IF there's an overlap and report a 1.
drug1_data:
PatientID StartDay StopDay
{'1008'} -2 0
{'1008'} 2 5
{'1008'} 9 9
{'1008'} 12 13
{'1008'} 16 16
{'1008'} 19 20
drug2_data:
PatientID StartDay StopDay
{'1008'} 1 11
{'1008'} 13 17
My piece of code:
if size(drug1_data, 1) > 0 && size(drug2_data,1) > 0
% if drug 2 was stopped before drug 1 started or vice versa, there's no overlap
% so if this is not the case, there is an overlap and we store a 1 in results
if ~(lt(drug1_data.StopDay, drug2_data.StartDay) || lt(drug2_data.StopDay, drug1_data.StartDay))
results(i,j) = 1;
end
end
But the tables have different heights and I get the obvious error: "Matrix dimensions must agree"
(i and j come from previous loops, and results is built as
results = zeros(length(unique_patients), length(DRUGS))
where DRUGS is the array of different drugs for the overlap comparison)
Giving that I'll use this code for hundreds of samples and they'll have all different heights all the time, how can I overcome this issue once and for all??
Thank you!

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Mayo de 2020
overlaps = ...
(drug1_data.StartDay <= drug2_data.StartDay.' & drug2_data.StopDay.' <= drug1_data.StopDay) | ...
(drug2_data.StartDay.' <= drug1_data.StartDay & drug1_data.StopDay <= drug2_data.StopDay.');
This will be an array that is height(drug1_data) by height(drug2_data) . In this case, 6 x 2, for the 6 courses of drug 1 and the 2 courses of drug 2.
any(overlaps, 1) will be a vector of length height(drug2_data) that is true where at least one course of drug1 overlaps with a course of drug2 (that is, will tell you which courses of drug 2 are overlapped). any(overlaps, 2) will be a vector of length height(drug1_data) that is true where at least one course of drug2 overlaps with course of drug1 (that is, will tell you which courses of drug 1 are overlapped). any(overlaps(:)) will be true if there is some overlap between drug 1 and drug 2.
  5 comentarios
Walter Roberson
Walter Roberson el 26 de Mayo de 2020
Which of the any() calls ended up being the one that was useful for you? Your original question suggested any(overlaps(:)) but I suspected that you might need the other information at some point.
camcecc10
camcecc10 el 26 de Mayo de 2020
any(overlaps(:)) was the one, in the end I need only one result for each patient

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by