arrays - 从数组中提取一定数量的 block

标签 arrays matlab

我有一个向量,我想从中提取所有 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/

相关文章:

arrays - 使用 D 中任意数量的元素初始化数组

matlab - 等值面函数 MATLAB 用法

MATLAB。一个脚本单元返回两个数字

matlab - 组合 2 个 MATLAB 结构体

Matlab 查询 : How to align 'title' at the botton of the figure when plotting points

matlab - 获取xy平面中三角形ROI中的所有点

c - 将数组(不是指针)传递给 c 中的结构

c - 当输入是阶数大于 2 的矩阵时,为什么会出现段错误?

c - 移动数组的列

java - 如何在 JAVA 中使用正则表达式从字符串中提取特定文本