Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Find position of the first place a value is reapeted.
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a large array larger than this, but I use it as an example. a=[0.9; 1; 0.6; 1.1; 0.2; 0.6; 1.1; 1; 1; 1; 1; 0.5; 0.2].
I want to find the index of the place where a number is reapeted severeal times and after each other.
So I want to find position 8.
0.6 is repeted but not after each other. 1 is the right value but I don't need it to find position 2.
Hope you understand my question. The numbers actually represent a change in number of points from a point cloud
댓글 수: 0
답변 (3개)
Tommy
2020년 4월 24일
How about
i = find(diff(a)==0,1);
댓글 수: 2
Tommy
2020년 4월 28일
Try this:
i=find([diff(a,2)==0;0]&diff(a)==0,1)
On your first example:
>> a=[0.9; 1; 0.6; 1.1; 0.2; 0.6; 1.1; 1; 1; 1; 1; 0.5; 0.2];
>> i=find([diff(a,2)==0;0]&diff(a)==0,1)
i =
8
And your second example:
>> a=[1; 2; 3; 3; 2; 4; 2; 1; 4; 6; 7; 8; 1; 1; 1; 1; 9];
>> i=find([diff(a,2)==0;0]&diff(a)==0,1)
i =
13
Adam Danz
2020년 4월 28일
편집: Adam Danz
2020년 5월 4일
This problem is not trivial, especially when working with floating decimals that potentially run into round-off error.
Here's a method below that returns the starting index of consecutve numbers with at least nConsec ocurrences in a row.
See inline comments for details.
% Create input vector (row or column)
a = [.9; 0.9; 1; 0.6; 1.1; 0.2; 1.1; 1.1; 1.1; 0.6; 1.1; 1; 1; 1; 1; 0.5; 0.2];
% Minimum number of consecutve values
nConsec = 3;
% Find consecutive numbers. This considers two numbers equal if their difference
% is less than 0.0000001.
% If you want to use exact matches: iseq = abs(a(1:end-1) - a(2:end)) == 0;
iseq = abs(a(1:end-1) - a(2:end)) < 0.0000001;
% Compute the index of the first of each consecutive string.
cLab = cumsum(diff([0;iseq(:)]) == 1) .* iseq(:); %concesutive labels
[counts,bin] = histcounts(cLab,1:max(cLab)+1);
startIdx = arrayfun(@(i)find(cLab==i,1,'first'), bin([counts+1>=nConsec,false]));
result
startIdx =
7 12
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!