Calculating Max and Min of subsets of data
9 views (last 30 days)
Show older comments
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
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
Accepted Answer
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
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
More Answers (2)
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
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!