Calculate the difference between two points extracted from a loop?
이전 댓글 표시
A is an array of [441000,1]. The values contained in this array are in the order:
a =
0
0
0
0
1
1
1
1
0
0
0
0
1
1
1
1
1
0
0
0
I have to find the difference between every starting 1 (i.e 1 after ever 0) and till the next xth position 1. (the position of first 1 - the position of xth 1 after the first series of 1).
So, first difference would be 101-1200 and then 1090-2202, 2200-3200 and so on. Kind of creating an overlap for every cutout.
In the data sample, there are approximately 1300 zeros after 100 ones. So I have to cut out the first one till the next 4th or 5th one and calculate the difference.
It feels like a simple code, but I am totally lost atm.
Thanks for your help!
댓글 수: 2
Guillaume
2016년 7월 19일
Can you please use valid matlab syntax for your example instead of some obscure notation that only you knows the meaning of.
What does the "difference between every starting 1 and till the next xth position 1" mean? The difference between 1 and 1 is always going to be 0, regardless of their position
What does "xth 1" mean? What is the value of x?
What your asking sounds like it could indeed be very simple (probably does not even need a loop) but it's really not clear at the moment.
automycer
2016년 7월 19일
채택된 답변
추가 답변 (1개)
Thorsten
2016년 7월 19일
x(101:103) = 1;
x(1090:1091) = 1;
x(1200:1201) = 1;
x(2201:2203) = 1;
x(3200) = 1;
istart = find([0 diff(x) == 1]);
i1 = find(x == 1);
iend = nan(1, numel(istart));
for i = 1:numel(istart)
i1g = i1(i1 > istart(i));
if numel(i1g) >= 5
iend(i) = i1g(5);
end
end
[istart' iend']
댓글 수: 2
Thorsten
2016년 7월 20일
If I understood you correctly, this will do the task:
istart = find([0 diff(x) == 1]);
iend = find([diff(x) == -1]);
% store the indices in a cell array
Cidx = arrayfun(@(i) istart(i):iend(i), 1:numel(iend), 'UniformOutput', false);
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!