How to find same nearby values and sort them

2 次查看(过去 30 天)
Hello, please help sort the task:
(example of data file below)
03/11/2016 2098.8 0
02/11/2016 2109.43 1
01/11/2016 2128.68 0
31/10/2016 2129.78 0
28/10/2016 2132.23 0
27/10/2016 2144.06 1
26/10/2016 2136.97 1
25/10/2016 2149.72 0
24/10/2016 2148.5 1
21/10/2016 2139.43 0
20/10/2016 2142.51 0
19/10/2016 2140.81 0
18/10/2016 2138.31 0
17/10/2016 2132.95 0
14/10/2016 2139.68 1
I need to sort out the data based on the third column. In particular, if we have 2,3,5,8 sets 1s or 0s I need to note the first date, last date, first and last date's numbers of second column, how many subsequent elements were there.
For example, starting from the top, output matrix for the first set will be:
01/11/2016 2128.68 28/10/2016 2132.23 3
P.S. additional question how to sort data file so that the dates would be ascending from top to bottom?
Thank you!
  7 个评论

请先登录,再进行评论。

采纳的回答

dpb
dpb 2017-2-19
编辑:dpb 2017-2-20
Given x is the third data column of [0 1...]
>> dx=diff(x)==0; % logical vector where difference is 0; ergo x(i+1)==x(i)
>> ix=[[dx;false] [false; dx]]; % arrange so here==next and here==previous in two columns
>> i1=find(ix(:,1) & ~ix(:,2)); % equals next but not previous (begin run)
>> i2=find(ix(:,2) & ~ix(:,1)); % equals previous but not next (end run)
>> [i1 i2 i2-i1+1] % display results -- start,stop,number for each group
ans =
3 5 3
6 7 2
10 14 5
>>
Use i1, i2 to build desired output array as wanted. Can do some combining if care to; kept intermediaries and doubled-up on the test so can see the "how"...
NB: The two logical tests can be written as
>> find(xor(ix(:,1),ix(:,2))).'
ans =
3 5 6 7 10 14
which returns the locations of each group start/stop in order. This may or may not be more convenient; to get the previous rearrange--
>> reshape(ans,2,[]).'
ans =
3 5
6 7
10 14

更多回答(1 个)

Jan
Jan 2017-2-12
编辑:Jan 2017-2-14
Start with importing the file by textscan. Then convert the first column using datenum, such that you can get sort them by:
[s, index] = sort(Date);
Then you can use the index to sort the complete array.
Finally explain with more details, what you want to get as output.
  1 个评论
Edmundas Povilavicius
Thank you for your help.
I assume the first part of your answer was about how to sort data file so that the dates would be ascending from top to bottom?
As for my main question, the "output" matrix should store a relevant sorted out data. Here's step by step guide of what I mean:
If we have 2,3,5,8 same numbers in a row in the third column - then we we take the first and last dates of the set, corresponding first and last second column numbers, and how many same numbers were there (either 2,3,5 or 8).
So going top to bottom through the example data set: we don't care about the first row, nor the second since they have just one 1 and one 0.
But we do start to care about the third row, because the fourth row has 0 in the third column as well (making it two 0s in a row(I do believe a better expression could be used here than 'in a row' to avoid confusion but hope you understand that I'm not referring to an actual row but rather a series of same numbers)).
The fifth row has 0 too, so now this 'set' becomes a size 3. We stop before the sixth row since it has 1 and the sequence is broken. As such, in a new matrix we store:
01/11/2016 2128.68 28/10/2016 2132.23 3 % first data, corresponding number, last date, corresponding number, sequence size.
Then we continue moving lower and we store sixth and seventh rows sits it has a sequence of two 1s.
Hope this is more clear now. Thank you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by