How can I separate data into multiple groups?
조회 수: 8 (최근 30일)
이전 댓글 표시
Hi,
I have a csv with more than 50,000 rows (an extract is provide in the attached csv file).
I need to group the data as highlighted in yellow in the attached file. The numbers in each group are either very close to each other (difference of less than 1) or they are multiple of the smaller number (with tolerance of +/- 0.3).
How can I write the code such that it can name the highlighted group as 1, 2, 3 and so on? For those number that don't belong to a group, 0 will be their default group number.
Thanks for the help in advance.
댓글 수: 2
Jan
2023년 3월 3일
CSV-files are text files. There are no colored elements.
Can you import the file already? Then you could start from "I have a vector" or "matrix".
답변 (1개)
Jan
2023년 3월 3일
편집: Jan
2023년 3월 3일
data = [2416.015, 127.402, 382.165, 127.425, 127.3387, 127.406, 637.001, 127.405, 2240.913, ...
2257.54, 241.801, 3064.636, 441.559, 220.805, 220.799, 1204.011, 1547.622, 322.37, ...
322.43, 6482.511, 558.603, 279.301, 2234.423, 279.307, 279.31, 279.295, 3901.168, ...
3595.353, 90.315];
m = [true, abs(diff(data)) < 1]; % Distance is small
ini = strfind(m, [0, 1]); % Index where blocks are starting
p = zeros(size(data));
p(ini) = 1;
p = cumsum(p); % Count starts
m(ini) = true;
result = m .* p; % Use m as mask
format long g
disp([data.', result.'])
댓글 수: 8
Jan
2023년 3월 7일
@Jayden Yeo: Yes, I've simplified my example. With the real data considering the tolerances will even increase the complexity.
If the desciption of the process is such tricky already, this is usually a hint, that the view on the problem is to indirect or contains too complicated assumptions. Therefore I ask, which real world problem you want to solve. Maybe there is a simpler solution to define groups.
참고 항목
카테고리
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!