Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Data extraction based on row specific criteria

Subject: Data extraction based on row specific criteria

From: Sarah Halliday

Date: 20 Jul, 2010 14:29:05

Message: 1 of 3

Hi,
Hoping someone can help me. I am working with a dataset which consists for 15 variables (columns) and up to 2000 samples (rows). I wish to extract a subset of the data which meets the following criteria:
• The value in column a must be greater than or equal to the 95th percentile of the whole column.
• The value in column a has to meet the above criteria for at least 3 consecutive rows.

I have written a function which works when I only have the first criteria.
function [ Event] = EventIden(Data, a, p1)
C1 = prctile(Data(:,a),p1);
Event = Data(Data(:,a)>=C1,:);
end

However, when I have attempted to extend this to try and extract only the rows where this condition is meet for three consecutive rows I can only get the code to output the last three rows where this condition is meet. I believe it is an error in the why am defining ‘Event’, but I am struggling to fix it.
 
function [ Event] = EventIden (Data, a, p1)
C1 = prctile(Data(:,a),p1);
m = length(Data(:,1))-2;
for n=1:m
    if Data(n,a)>=C1 && Data(n+1,a)>=C1 && Data(n+2,a)>=C1
        Event = Data(n:n+2,:);
    end
end

Any help you could provide would be greatly appreciated,

Kind Regards
Sarah

Subject: Data extraction based on row specific criteria

From: Sean

Date: 20 Jul, 2010 15:52:05

Message: 2 of 3

"Sarah Halliday" <s.j.halliday@student.reading.ac.uk> wrote in message <i24brh$ge6$1@fred.mathworks.com>...
> Hi,
> Hoping someone can help me. I am working with a dataset which consists for 15 variables (columns) and up to 2000 samples (rows). I wish to extract a subset of the data which meets the following criteria:
> • The value in column a must be greater than or equal to the 95th percentile of the whole column.
> • The value in column a has to meet the above criteria for at least 3 consecutive rows.
>
> I have written a function which works when I only have the first criteria.
> function [ Event] = EventIden(Data, a, p1)
> C1 = prctile(Data(:,a),p1);
> Event = Data(Data(:,a)>=C1,:);
> end
>
> However, when I have attempted to extend this to try and extract only the rows where this condition is meet for three consecutive rows I can only get the code to output the last three rows where this condition is meet. I believe it is an error in the why am defining ‘Event’, but I am struggling to fix it.
>
> function [ Event] = EventIden (Data, a, p1)
> C1 = prctile(Data(:,a),p1);
> m = length(Data(:,1))-2;
> for n=1:m
> if Data(n,a)>=C1 && Data(n+1,a)>=C1 && Data(n+2,a)>=C1
> Event = Data(n:n+2,:);
> end
> end
>
> Any help you could provide would be greatly appreciated,
>
> Kind Regards
> Sarah

%I'm assuming column a is your first one?
%If so:

M = your_matrix;
[Msorted idx] = sortrows(M,1); %sort along the first column
my_95th = ceil(size(M,1)/20*19); %part to keep
Msorted = Msorted(my_95th:end,:); %keep only 95th and above
idx = diff(idx(my_95th:end))==1; %find rows that are touching in 95th percentile (difference in position == 1)
idx = strfind(idx',[1 1 1]);
idx = unique([idx idx+1 idx+2]);% get 3 adjacent values; unique in case there are more than 3 in a row
M2 = Msorted(Idx,:); %Keep selected rows

Subject: Data extraction based on row specific criteria

From: Sean

Date: 20 Jul, 2010 15:57:05

Message: 3 of 3

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
> M = your_matrix;
> [Msorted idx] = sortrows(M,1); %sort along the first column
> my_95th = ceil(size(M,1)/20*19); %part to keep
> Msorted = Msorted(my_95th:end,:); %keep only 95th and above
small mistake here: idx should be sorted before the difference i.e.
> idx = diff(sort(idx(my_95th:end)))==1; %find rows that are touching in 95th percentile (difference in position == 1)

> idx = strfind(idx',[1 1 1]);
> idx = unique([idx idx+1 idx+2]);% get 3 adjacent values; unique in case there are more than 3 in a row
> M2 = Msorted(Idx,:); %Keep selected rows

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us