Matlab之谜:double和datetime数组之间没有定义比较

时间:2016-09-25 12:11:43

标签: matlab

我是Matlab的新手,通过尽可能简单地将其删除来排除比较错误。这两行测试矩阵工作正常,然后我复制/粘贴添加第三条记录,我得到一个转换错误(对于相同的数据)!

背景:我的数据用于患者检查:患者,到达时间,考试开始时间,我正在编写一个程序来确定等候室的充分程度。如果没有立即看到患者,我将它们添加到patient_queue(仅将其最终开始时间保存为占位符)。数据按到达时间顺序排列,因此在每个新行我检查队列,看看是否有人在此期间开始检查,删除它们,然后从那里开始。

以下是仅有两个患者行的示例(此作品):

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

但是当我使用完整的数据集时,我会在行中收到错误:

if patient_queue{j} < now
  

double和datetime数组之间没有定义比较。

所以我正在缩小问题范围,我甚至可以通过使用2个记录工作的简单矩阵重现错误,复制第二个记录以制作3的矩阵,就像这样 - 只需在上面的代码中交换它发出错误(!!):

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]

我错过了什么?

2 个答案:

答案 0 :(得分:1)

这是一种更简短的方法,无需将数字转换为日期:

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

答案 1 :(得分:0)

右括号有什么不同:/

我已将此声明为向量而不是单元格数组: patient_queue = [];

结果,我无法达到内部的价值。

答案是:   - 以这种方式声明:patient_queue = {};   - 为patient_queue {j}

添加一个(缺少的)日期时间转换

这是我犯的一对特定错误,所以我会删除帖子,因为它不太可能对其他人有用