Find datapoints a specified distance apart in matrix using while loop
1 次查看(过去 30 天)
显示 更早的评论
I have some data with positive integers (to_pos). I want to find and extract into a second matrix (spacedCrossPoints) datapoints that are a minimum of 30 from each other. So my desired output would be: 1214,2263,2320,8262,8292....
Here is my not working code so far:
gapBetweenCrosses = 30;
for cross = 2:length(t0_pos)-1;
i = cross;
if (((t0_pos(cross+1))-(t0_pos(i))) >= gapBetweenCrosses)% && (cross < ((length(t0_pos))-30)); %if gap between row greater than or equal to 30(s)
spacedCrossPoints(end+1,1) = t0_pos(cross+1); %insert values of crosspoints into matrix
elseif ((t0_pos(cross+1))) - (t0_pos(i)) < gapBetweenCrosses %&& cross < (length(t0_pos))-30; %if gap is less than 30. skips
%jump = 30-(((t0_pos(cross+1))) - (t0_pos(cross))); %calculate jump as difference between 30 and different between rows
while ((t0_pos(cross+1))) - (t0_pos(i)) < gapBetweenCrosses
cross = cross+1;
%spacedCrossPoints(end+1,1) = t0_pos(cross+1); %notes new value 30 past last value %NEED TO MAKE CROSS NOT EXCEED length t0_pos
end
end
cross = cross+1;
end
end
2 个评论
Dyuman Joshi
2023-3-27
"I want to find and extract into a second matrix (spacedCrossPoints) datapoints that are a minimum of 30 from each other."
How does the above statement relate to this - "So my desired output would be: 1214,2263,2320,8262,8292...."
Do you want to obtain all the pair of points whose difference is >= gap?
采纳的回答
Dyuman Joshi
2023-3-27
编辑:Dyuman Joshi
2023-3-27
Points which are >= 30 than the previous point starting with the 1st point -
load t0_pos.mat
gap=30;
k=1;
y=false(size(t0_pos));
y(k)=true;
%loop
while k<numel(t0_pos)
k=find(t0_pos - t0_pos(k) >= gap, 1);
y(k)=true;
end
out = t0_pos(y)'
3 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!