From: Sarah Halliday on
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
From: Sean on
"Sarah Halliday" <s.j.halliday(a)student.reading.ac.uk> wrote in message <i24brh$ge6$1(a)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:
> &#8226; The value in column a must be greater than or equal to the 95th percentile of the whole column.
> &#8226; 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 &#8216;Event&#8217;, 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
From: Sean on
"Sean " <sean.dewolski(a)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