How to get rid of a row if one column is less than a certain value?

3 views (last 30 days)
I have a large data set and need to remove rows of a cell if columns 2 or 3 are less than 10. This is my current script:
clear all
clc
[F,S,R] = xlsread('Matlabhelp.csv');
t = F(:,1);
date = datetime(t, 'ConvertFrom','excel')
% time = F(:,2)/24;
dv = datenum(date);
[dv_sort,idx] = sort(dv);
FinalSorted_1 = [datevec(dv_sort) F(idx,2) F(idx,3)];
FinalSorted_2 = table(datetime(datevec(dv_sort)), F(idx,2), F(idx,3));
acceleration= table2cell(FinalSorted_2);
  1 Comment
KPSil
KPSil on 14 Apr 2018
https://uk.mathworks.com/matlabcentral/answers/394898-excel-matrix-sorting-to-identify-positions

Sign in to comment.

Accepted Answer

KSSV
KSSV on 9 Apr 2018
Let A be your matrix.
% Remove rows if column 2 less then 10
idx = A(:,3)<10 ;
A(idx,:) = [] ;
  2 Comments
KPSil
KPSil on 9 Apr 2018
Adding your answer into script:
clear all
clc
[F,S,R] = xlsread('Matlabhelp.csv');
t = F(:,1);
date = datetime(t, 'ConvertFrom','excel')
% time = F(:,2)/24;
dv = datenum(date);
[dv_sort,idx] = sort(dv);
FinalSorted_1 = [datevec(dv_sort) F(idx,2) F(idx,3)];
FinalSorted_2 = table(datetime(datevec(dv_sort)), F(idx,2), F(idx,3));
A= table2cell(FinalSorted_2);
% Remove rows if column 2 less then 10
idx = A(:,3)<10 ;
A(idx,:) = [] ;
The following error occurs:
Undefined operator '<' for input arguments of type 'cell'.
Error in rough (line 17)
idx = acceleration(:,3)<10 ;
My A is in a cell format so not sure if this would affect it?
Thanks for your response
KPSil
KPSil on 14 Apr 2018
https://uk.mathworks.com/matlabcentral/answers/394898-excel-matrix-sorting-to-identify-positions

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 9 Apr 2018
You started with a table which are a lot easier to work with than cell arrays, so get rid of the table2cell. For that matter, a lot of the gymnastic could be removed if you used readtable to load the data directly into a table, rather than xlsread. Assuming you're on a recent enough version of matlab, readtable knows how to read dates directly, unlike xlsread.
So:
acceleration = readtable('Matlabhelp.csv');
is probably all that's needed to read the file. If not, post an example of the file. Once that is done, sorting is trivially done:
acceleration = sortrows(acceleration);
and filtering:
acceleration(any(acceleration{:, [2 3]} < 10, 2), :) = []; %delete rows for which any of column 2 or 3 are less than 10

Tags

Products

Community Treasure Hunt

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

Start Hunting!