Ordering data based on previous data value
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
Hello,
I have two columns of data as below:
Column 1 Column 2
39.28 12.45
42.85 0.00
48.14 0.00
53.67 9.21
42.80 16.09
0.00 0.00
Starting ith the data in row 6 (0.00 0.00), how can I select the row of data which contains the number in column 1 that is closest to 0 i.e.
0.00 0.00, 39.28 12.45
Then, once I've found the above, I then need to find the next row of data using Column 1 data that is closest to 12.45 i.e.
0.00 0.00, 39.28 12.45, 42.80 16.09
And then so on and so on until I get:
0.00 0.00, 39.28 12.45, 42.80 16.09, 42.85 0.00, 53.67 9.21.
There can be no repeat rows of data.
Many thanks,
Phil
댓글 수: 4
Bob Thompson
2018년 12월 3일
Do you need to maintain the current order of the data? This would be fairly easy if you just sorted with sortrows() and then progress from top to bottom.
Phil Roberts
2018년 12월 4일
Bob Thompson
2018년 12월 4일
I'm confused how you went from 16.09 to 0.00, rather than 9.21.
Phil Roberts
2018년 12월 4일
답변 (1개)
Guillaume
2018년 12월 4일
You will have to use an iterative method to do what you want.
m = [39.28 12.45; 42.85 0.00; 48.14 0.00; 53.67 9.21; 42.80 16.09; 0.00 0.00] %demo data
sortedm = zeros(size(m));
lastval = 0;
for destrow = 1:size(m, 1)
[~, moverow] = min(abs(m(:, 1) - lastval));
sortedm(destrow, :) = m(moverow, :);
lastval = m(moverow, 2);
m(moverow, :) = [];
end
You may be able to avoid the iterative deletion of elements (most likely the slowest part of the algorithm) by using a logical array as a row filter but you'd have to relate the location of filtered rows to the full array.
댓글 수: 0
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!