Find the index range of ones/trues?

조회 수: 18 (최근 30일)
Yi-xiao Liu
Yi-xiao Liu 2021년 4월 14일
댓글: Walter Roberson 2021년 4월 15일
For example, given a logical vector:
[0,0,1,0,0,1,1,0,1,1,1,0,0...]
I need a two-column matrix:
[3,3;
6,7;
9,11;...]
that register the start and end index of each "1 blocks". Is there any convenient way to do that (ideally vectorized)?

채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 14일
test_vector = [0,0,1,0,0,1,1,0,1,1,1,0,1,1,1,1];
starts = strfind([0 test_vector], [0 1]);
stops = strfind([test_vector 0], [1 0]);
out = [starts(:), stops(:)]
out = 4×2
3 3 6 7 9 11 13 16
  댓글 수: 2
Yi-xiao Liu
Yi-xiao Liu 2021년 4월 14일
Brilliant, I didn't know strfind operates on numerical arrays
Walter Roberson
Walter Roberson 2021년 4월 15일
It isn't documented, but it is a long-standing trick that is very convenient.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

SungJun Cho
SungJun Cho 2021년 4월 14일
편집: SungJun Cho 2021년 4월 14일
I made a brief function that gives you a two-column matrix of the start and end indices when a logical vector is given as an input. I used the iteration method here, but I am pretty sure there is a recursion method to solve this problem. Hope this helps.
test_vector = [0,0,1,0,0,1,1,0,1,1,1,0,1,1,1,1];
output = get_true(test_vector);
function [output] = get_true(test_vector)
test_array = find(test_vector == 1);
test_idx = zeros(length(test_array)-1,2);
for ii = 1:length(test_array)
if ii == length(test_array)
break;
end
test_idx(ii,1) = test_array(ii);
test_idx(ii,2) = test_array(ii+1);
end
output = zeros(length(test_idx),2);
for idx = 1:length(test_idx)
idx_start = test_idx(idx,1);
idx_end = test_idx(idx,2);
if idx_start+1 < idx_end
output(idx,1) = idx_start;
output(idx,2) = idx_end;
end
end
output = sort(output(output~=0));
output = vertcat(test_array(1), output, test_array(end));
output = reshape(output,2,length(output)/2)';
end
Best,
SungJun

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by