gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:22:44 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com