arrays - 在 Matlab 中将数组映射到逻辑数组

标签 arrays matlab indexing

比方说 array a=[1,3,8,10,11,15,24] ,以及 logical array b=[1,0,0,1,1,1,0,0,0,1,1,1,1,1] ,如何获取[1,1,3,1,3,8,1,3,8,1,2,3,8,10] ,查看 b 中逻辑变为 1 的位置,数组索引a重置,因此它从头开始,逻辑变为 0 a array 的情况也相同从头开始并继续 1,3,8,10..etc.

最佳答案

您可以使用 diff 查找 b 发生变化的位置,然后使用 arrayfuna 生成索引:

a=[1,3,8,10,11,15,24];
b=[1,0,0,1,1,1,0,0,0,1,1,1,1,1];
idxs = find(diff(b) ~= 0) + 1; % where b changes
startidxs = [1 idxs];
endidxs = [idxs - 1,length(b)];
% indexes for a
ia = cell2mat(arrayfun(@(x,y) 1:(y-x+1),startidxs,endidxs,'UniformOutput',0));
res = a(ia);

关于arrays - 在 Matlab 中将数组映射到逻辑数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43494452/

相关文章:

r - 矩阵中的索引元素和相应的列号

java是否循环复制对象?

javascript - 从 JavaScript 中的关联数组中获取第一项的最有效方法是什么?

PHP 不区分大小写等价于 strtr

matlab - 单击按钮时是否按下了 'Ctrl' 键?

mysql - mysql复合索引会让其他一些索引完全多余吗?

php - 如何使用特定列名作为数组索引来获取特定列的结果?

matlab - 如何在 MATLAB 中创建相机飞越效果?

matlab - 在 MatLab 中创建翻译器

sql-server - SQL Server 数据库中所有索引和索引列的列表