我有一个1s和0的二进制向量。我想找一个数字1的函数范围/岛。例如:X=00011110011111000110.我需要一个这样的答案: 4-7 (或45-67),10-16,20-21 .谢谢你的帮助!
发布于 2013-09-20 06:10:59
Aki解决方案的变体(测试不多):
x = [0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 0];
dx = diff([0, x, 0]);
start_pos = find(dx == 1);
end_pos = find(dx == -1) - 1;发布于 2013-09-20 05:35:36
向原始数组的两端添加零可以保证转换的偶数(从0开始到1开始,结尾从1到0)。这基本上是一个diff和细化输出的问题。
x = [0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 1 1 0];
% how to make that out from a string xx="0001111001111111000110" is left
% as an exercise
y = [0 x 0]; % make sure x="11"; has proper amount of transitions
R = 1:length(y)-1; % make an array of indices [1 2 3 4 5 ... end-1]
F = R(y(2:end) != y(1:end-1)); % finds the positions [4,8,10,17,20,22]
start_pos = F(1:2:end-1); % gets 4,10,20
end_pos = F(2:2:end)-1; % gets 7,16,21 adjusted免责声明:未经测试。
https://stackoverflow.com/questions/18909268
复制相似问题