我有一个向量,我想从中提取所有 4:
x = [1 1 1 4 4 5 5 4 6 1 2 4 4 4 9 8 4 4 4 4]
这样我将得到 4 个向量或一个包含 4 的 4 个 block 的单元格:
[4 4], [4], [4 4 4], [4 4 4 4]
谢谢!
最佳答案
您可以使用 arrayfun
从适当的范围创建单元格:
x = [1 1 1 4 4 5 5 4 6 1 2 4 4 4 9 8 4 4 4 4];
x = [0, x, 0]; D = diff (x==4); % pad array and diff its mask
A = find (D == 1); B = find (D == -1); % find inflection points
out = arrayfun (@ (a,b) {x(a+1 : b)}, A, B) % collect ranges in cells
关于arrays - 从数组中提取一定数量的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38948544/