带有 for 循环实现的 matlab tall 数组

matlab tall arrays with for loop inplementation

我正在处理大文件 (30GB+) 中的二进制数据。 我已经实现了带有自定义读取功能的 fileDatastore。

fds = fileDatastore(location,'ReadFcn',@readFile);

readFile 函数将二进制数据一次一个字节读入数组。 然后我从 fileDatastore 创建一个 tall 数组。

data = tall(fds);

这似乎一切正常。现在我想在 tall 数组中搜索字节模式(例如 25 后跟 30)。像...

for i=1:size(data)
    if data(i) == 25 && data(i+1) == 30
        disp('do something')
    end
end

这似乎不可能。我最终遇到了 'conversion to logical from tall is not possible' 错误。我该如何解决这个问题?

正如 excaza 提到的,问题是尽管 data(i) == 25 有效,但它没有返回布尔值。所以你不能应用 && 运算符。 简单的解决方案似乎是嵌套 if 语句...

if data(i) == 25 
   if data(i+1) == 30
        disp('do something')
    end
end

基本上,您似乎需要做的是迭代数据的子集,因为它太小而无法放入内存。例如,如果您设置

y = gather(data(1:100))

然后您可以对 y 而不是数据进行您想要的比较。重要的一点是 y 必须适合内存,而 data 则不能。所以你可能不能(例如)设置

y = gather(data)

问题是您不能将高逻辑值用作循环语句中的条件。这就是为什么错误是 "Conversion to logical from tall is not possible." 所以因为 data(i) == 25 returns 是一个高逻辑,你只需要使用 gather 把它变成一个内存逻辑。试试这个(因为同时收集这些可能更有效):

[a,b] = gather(data(i)==25,data(i+1)==30);
if a && b
    disp('do something')
end

Source: http://www.mathworks.com/help/matlab/import_export/deferred-evaluation-of-tall-arrays.html

"For example, you cannot control if or while loop statements using a tall logical array, but once the array is evaluated with gather it becomes an in-memory logical value that you can use in these contexts."