Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 提取数组中大于零的值集的索引_Arrays_Matlab_Indexing_Find - Fatal编程技术网

Arrays 提取数组中大于零的值集的索引

Arrays 提取数组中大于零的值集的索引,arrays,matlab,indexing,find,Arrays,Matlab,Indexing,Find,我有一个长度为n的数组。该数组具有制动能量值,索引数表示时间(以秒为单位) 阵列的结构如下所示: 索引1到140,数组具有零值。(车辆未制动) 索引141到200,数组具有随机能量值。(车辆处于制动和再生能量状态) 索引201到325,数组具有零值。(车辆未制动) 索引326到405,数组具有随机能量值。(车辆正在制动和再生能量) …对于长度为n的数组,依此类推 我想做的是得到每组能量值的开始和结束索引号。 例如,上述序列给出了以下结果: 141 - 200 326 - 405 ...

我有一个长度为
n
的数组。该数组具有制动能量值,索引数表示时间(以秒为单位)

阵列的结构如下所示:

  • 索引1到140,数组具有零值。
    (车辆未制动)

  • 索引141到200,数组具有随机能量值。
    (车辆处于制动和再生能量状态)

  • 索引201到325,数组具有零值。
    (车辆未制动)

  • 索引326到405,数组具有随机能量值。
    (车辆正在制动和再生能量)

…对于长度为
n
的数组,依此类推

我想做的是得到每组能量值的开始和结束索引号。

例如,上述序列给出了以下结果:

141 - 200
326 - 405
...   

有人能建议我使用什么方法或技巧来获得这个结果吗?

使用
diff
是一种快速的方法

这是一个演示(有关详细信息,请参见注释):


因此,您可以按自己的喜好设置格式,以获得跨度
4-6、9-14

此任务由两种方法执行,两种方法都非常有效

沃尔夫方法:

bChange = diff( EnergyB > 0 );
startIdx = find( bChange > 0 ) + 1;  % Indices of [0 1] pairs
endIdx = find( bChange < 0 );        % Indices of [1 0] pairs
endIdx=

第二种方法:

startends = find(diff([0; EnergyB > 0; 0]));
startends = reshape(startends, 2, [])';
startends(:, 2) = startends(:, 2) - 1
结果:

bChange = diff( EnergyB > 0 );
startIdx = find( bChange > 0 ) + 1;  % Indices of [0 1] pairs
endIdx = find( bChange < 0 );        % Indices of [1 0] pairs
明星=


非常感谢。我使用了你的方法,也设法用另一种方法执行任务。两者都很方便。我将在这里分享这两者。
     141
     370
     608
     843
     212
     426
     642
     912
startends = find(diff([0; EnergyB > 0; 0]));
startends = reshape(startends, 2, [])';
startends(:, 2) = startends(:, 2) - 1
     141         212
     370         426
     608         642
     843         912