Diane Kaplan
Diane Kaplan

Reputation: 1708

Matlab mystery: Comparison is not defined between double and datetime arrays

I'm new to Matlab, troubleshooting a comparison error by chopping it down as simple as can be. This test matrix of two rows works fine, then I copy/paste to add a third record and I get a conversion error (for the same data)!

Background: my data is for patient exams: patient, arrival time, exam start time, and I'm writing a program to determine how full the waiting room gets. If the patient isn't seen instantaneously I add them to a patient_queue (just saving their eventual start time as a placeholder). The data is in order of arrival time, so at each new row I check the queue to see if anyone's exam started in the meantime, remove them, and go from there.

Here's an example with only two patient rows (this works):

data_matrix =  [1,735724.291666667,735724.322916667,735724.343750000,5;
    2,735724.331250000,735724.340277778,735724.371527778,18];

patient_queue = [];
highest_wait_num = 0;
[rows, columns] = size(data_matrix);

for i = 1:rows
    this_row_arrival = datetime (data_matrix(i, 2), 'ConvertFrom', 'datenum');
    this_row_exam_start = datetime (data_matrix(i, 3), 'ConvertFrom', 'datenum');
    now = this_row_arrival;  %making a copy called 'now' for readability below

    % check patient queue: if anyone left in the meantime, remove them
    for j = 1:length(patient_queue)
        if patient_queue{j} < now
            patient_queue(j) = [];
        end
    end

    % if new patient isn't seen immediately (tb > ta), add tb to the queue
    if this_row_exam_start > this_row_arrival
        patient_queue{end+1} = this_row_exam_start;
    end

    % get the current queue size
    patient_queue_non_zero = (~cellfun('isempty',patient_queue));
    indices = find(patient_queue_non_zero);
    current_queue_count = length(indices);

    % if the current queue size beats the highest we've seen, update it
    if current_queue_count > highest_wait_num
        highest_wait_num = current_queue_count;
    end
end

patient_queue{j}
highest_wait_num

But when I use the full data set I get an error at the line:

if patient_queue{j} < now

Comparison is not defined between double and datetime arrays.

So I'm narrowing the problem, and I can even reproduce the error by taking my simple matrix of 2 records that worked, copy the second one to make a matrix of 3, like so- just swapping that in the code above makes the error(!!):

data_matrix =  [1,735724.291666667,735724.322916667,735724.343750000,5;
    2,735724.331250000,735724.340277778,735724.371527778,18;
    2,735724.331250000,735724.340277778,735724.371527778,18]

What am I missing?

Upvotes: 0

Views: 1336

Answers (2)

EBH
EBH

Reputation: 10440

Here is a much shorter way to do this, without converting numbers to dates:

patient_queue = [];
highest_wait_num = 0;
rows = size(data_matrix,1);

for k = 1:rows
    this_row_arrival = data_matrix(k, 2);
    this_row_exam_start = data_matrix(k, 3);
    now = data_matrix(k, 2);  %making a copy called 'now' for readability below

    % check patient queue: if anyone left in the meantime, remove them
    patient_queue(patient_queue < now) = [];

    % if new patient isn't seen immediately (tb > ta), add tb to the queue
    if this_row_exam_start > this_row_arrival
        patient_queue(end+1) = this_row_exam_start;
    end

    % get the current queue size
    current_queue_count = numel(nonzeros(patient_queue));

     % if the current queue size beats the highest we've seen, update it
     highest_wait_num = max(current_queue_count,highest_wait_num);
end

Upvotes: 1

Diane Kaplan
Diane Kaplan

Reputation: 1708

What difference the right braces make :/

I had declared this as a vector instead of a cell array: patient_queue = [];

As a result, I couldn't get to the value inside.

The answer was to: - declare it this way instead: patient_queue = {}; - add one more (missing) datetime conversion for patient_queue{j}

This is a specific pair of mistakes I had, so I'll delete the post since it's unlikely to be useful for others

Upvotes: 0

Related Questions