Calculating Max and Min of subsets of data

9 views (last 30 days)
Hi,
I have 8 columns of 63000 data points that I want to calculate the max and min every 57, 56, 56 ,56, 57, 56,... subset.
I know there should be an easier way then doing several repeating loop layers to take these subsets of varying length and calculating the max and min.
Any help would be most appreciative.
Cheers,
Elliott
Edit: not that it matters much but it's actually 63553 x 7, I was counting my timestamp data in the 8th column, but I don't need the averages of that.
  1 Comment
Andrei Bobrov
Andrei Bobrov on 20 Sep 2012
A = randi(78,63553,7);
a = [57, 56, 56 ,56].';
s = size(A);
sa = sum(a);
t = cumsum(a) < rem(s(1),sa);
i1 = [repmat(a,fix(s(1)/sa),1);a(t);rem(s(1),sa) - a(t)];
i4 = cumsum(i1);
i3 = i4 - i1 + 1;
Aminmax = zeros(numel(i3),s(2),2);
for jj = 1:numel(i3)
d = A(i3(jj):i4(jj),:);
Aminmax(jj,:,:) = cat(3,min(d),max(d));
end

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 19 Sep 2012
Edited: Azzi Abdelmalek on 19 Sep 2012
x=rand(63000,8);
id1= 3*56+57 ;
s=reshape(x',8,id1,[]);minx=[];
for k=1:size(s,3)
minx=[minx min(min(s(:,1:57,k))) min(min(s(:,58:113,k)))...
min(min(s(:,114:169,k))) min(min(s(:,170:225,k)))];
maxx=[maxx max(max(s(:,1:57,k))) max(max(s(:,58:113,k)))...
max(max(s(:,114:169,k))) max(max(s(:,170:225,k)))]
end
  7 Comments
Elliott Brecht
Elliott Brecht on 19 Sep 2012
Edited: Elliott Brecht on 19 Sep 2012
Hmmm.. I'm getting more undefined errors and the subsets seem small and have elements that I'm not sure where it's getting them, I know what the Max and Min should be for several of the subsets and I'm getting different results.
Azzi Abdelmalek
Azzi Abdelmalek on 19 Sep 2012
I think it's better if we cut then adding nan, because nan or zeros will change min max values
clear
n=63553
y=rand(n,8);
id1= 3*56+57;
c=floor(n/id1)
if c~=n/id1
x=y(1:c*id1,:);
xr=y(c*id1+1:end,:);
else
x=y
xr=[];
end
% x is 63450x8 and xr is 103x8

Sign in to comment.

More Answers (2)

Laura Proctor
Laura Proctor on 19 Sep 2012
Is your matrix 7875 x 8?
Also, when you say "every 57" does that mean the first 57 rows? The first 57 elements in the column? The first 57 elements going row wise?
If you, for example, wanted to calculate the max/min for the first 57 rows, the syntax is simply:
xMax1 = max(x(1:57,:));
xMin1 = min(x(1:57,:));
  2 Comments
Elliott Brecht
Elliott Brecht on 19 Sep 2012
My Matrix is 63553 x 8 I want to separately get the max, min of the first 57 elements of each column, then the next 56 elements, next 56 elements, etc. repeated until end of data set.
Elliott Brecht
Elliott Brecht on 19 Sep 2012
Edited: Elliott Brecht on 19 Sep 2012
This works great, but it seems I would need to loop and still calculate each subset. Is there a way to automate the numbers, xMax1 = max(x(1:57,:)) max(x(58:113,:)), continued, continued without defining the matrix locations as variables and increasing them in a loop?

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 19 Sep 2012
Edited: Azzi Abdelmalek on 20 Sep 2012
%corrected code
cclear
n=63553
y=rand(n,8);
id1= 3*56+57;
c=floor(n/id1)
if c~=n/id1
x=y(1:c*id1,:);
xr=y(c*id1+1:end,:);
else
x=y
xr=[];
end
s=reshape(x',8,id1,[]);minx=[];maxx=[]
for k=1:size(s,3)
i1=1:57,i2=58:113,i3=114:169,i4=170:225;
minx=[minx min(min(s(:,i1,k))) min(min(s(:,i2,k)))...
min(min(s(:,i3,k))) min(min(s(:,i4,k)))];
maxx=[maxx max(max(s(:,i1,k))) max(max(s(:,i2,k)))...
max(max(s(:,i3,k))) max(max(s(:,i4,k)))];
end
idxr=size(xr,1);test=57;
idxr1=idxr
x0=1
while idxr1-test>0
xr1=xr(x0:x0+test-1,:)
minx=[minx min(min(xr1))];
maxx=[minx max(max(xr1))];
idxr1=idxr1-test
x0=x0+test;
test=56;
end
if idxr1>0
xr1=xr(x0:end,:)
minx2=[minx min(min(xr1))];
maxx2=[minx max(max(xr1))];
end
% or using only "while"
clear
n=63553;
xr=rand(n,8);
load ansm
xr=y;
numx=round(n/(3*56+57))+1;
idxr1=size(xr,1);
v=repmat([57 56 56 56],1,numx);
k=1;minx=[];maxx=[];test=57;x0=1;
while idxr1-test>0
xr1=xr(x0:x0+test-1,:);
minx=[minx min(min(xr1))];
maxx=[minx max(max(xr1))];
idxr1=idxr1-test
k=k+1;x0=test+x0;
test=v(k);
end
if idxr1>0
xr1=xr(x0:end,:);
minx1=[minx min(min(xr1))];
maxx1=[minx max(max(xr1))];
end
  2 Comments
Elliott Brecht
Elliott Brecht on 20 Sep 2012
Edited: Elliott Brecht on 20 Sep 2012
Thanks! Made some minor edits but it worked great. Thanks a ton!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!