1 arrays algorithm matlab matrix-indexing
如果我有序列1 0 0 0 1 0 1 0 1 1 1
如何有效地定位从两侧1的零.
在这个序列中,它表示位置6和8为零.粗体表示零.
1 0 0 0 1 0 1 0 1 1 1
我可以想象算法会循环遍历数组,然后看一个在后面,一个在前面我猜这意味着O(n)所以可能没有更平滑的算法.
如果你能找到另一种方式,我感兴趣.
用途strfind
:
pos = strfind(X(:)', [1 0 1]) + 1
Run Code Online (Sandbox Code Playgroud)
请注意,这仅在X
向量时才有效.
X = [1 0 0 0 1 0 1 0 1 1 1 ];
pos = strfind(X(:)', [1 0 1]) + 1
Run Code Online (Sandbox Code Playgroud)
结果:
pos =
6 8
Run Code Online (Sandbox Code Playgroud)