Check if Column has Correct Sequence of Numbers
10 views (last 30 days)
Show older comments
Spencer Ferris
on 25 Feb 2021
Commented: Spencer Ferris
on 25 Feb 2021
I have a column of motion tracking data where there are supposed to only be 3 values (integers) and they should repeat in the same order every 3 rows.
For example, the order is 52630, 1, 2. It repeats ilike that for a few thousand lines.
Once or twice in the column, it will skip one of these values. I need to be able to detect when this happens, and if possible remove the rows from the table where it happens.
For example, if there is a 1 missing in the sequence, I need to be able to delete the rows that containt the 52630 and 2 that are missing the 1 in between them.
I've tried a few different things but none seemed to work as I expected them to.
Thanks!
0 Comments
Accepted Answer
KALYAN ACHARJYA
on 25 Feb 2021
Edited: KALYAN ACHARJYA
on 25 Feb 2021
Steps:
1.Create the complete Table with original data (including missing data)
3. Get the rows from the logical matrix of the result of step 2
[r,~]=find(result3==1);
4. Remove all those rows from the table
table_varible_name(r,:)=[]
3 Comments
KALYAN ACHARJYA
on 25 Feb 2021
Edited: KALYAN ACHARJYA
on 25 Feb 2021
As per the given attached data here an example
data=repmat([52630 1 2],[1,5]);
data(8)=[]; % Creation of sample data
T=table(data')
T =
14×1 table
Var1
_____
52630
1
2
52630
1
2
52630
2
52630
1
2
52630
1
2
First Option: (Hard Coding)
result=diff(T.Var1)
Here you have to make a comparison in the result data or use ~"find" function to those undesired results, as you have known patterns, you know the diff results of consecutive numbers.
result =
-52629
1
52628
-52629
1
52628
-52628
52628
-52629
1
52628
-52629
1
See index 7 number -52628 is not matched with pattern diff results.
Second Option
idx = findpattern(T.Var1,[52630 1 2])
idx =
1 4 9 12
idx_final=sort([idx,idx+1,idx+2]); % This you can generelize;
% it gives the 1st index of the pattern matched, three number in the pattern
% Hence I have added extra 2 to get the indices
% Total addition required length(pattern_array)-1
idx_final =
1 2 3 4 5 6 9 10 11 12 13 14
Now find the indices which are not present the the idx_final indices
>> find(ismember(1:length(T.Var1),idx_final)==0)
%.....................^ Rows number in the table
ans =
7 8
Third Option: normxcorr(): I have not tested with the sample given data
Fourth Way: There are multiple ways, choose as per your requirements and understanding
Once you get the rows deletion is easy
table_variable(idx,:)=[]
Hope it Helps :)
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!