Scanning an entire table in blocks of 60s for max and min values

a={rand(1877958,7); rand(1251972,7)};
b=cellfun(@(x) [x; repmat(x(end,:),-mod(size(x,1),-60),1)],a,'un',0)
for i=1:length(b)
mn(i)={max(reshape(b{i}(:,4),60,[])).'};
mx(i)={min(reshape(b{i}(:,5),60,[])).'};
end
Some good people on this forum gave me this code. This code allows the data to be grouped into rows of 60s and then find the max and min value. This code starts at row 1 to 60, 61 to 120, 121 to 180 and so on.
I want the cell array to be expanded and also look at: max and min value of rows 2 to 61, 62 to 121 and so on. max and min value of rows 3 to 62, 63 to 121 and so on. ... max and min value of rows 58 to 117, 118 to 177 and so on. max and min value of rows 59 to 118, 119 to 178 and so on. max and min value of rows 60 to 119, 120 to 179 and so on. STOP
How can I add this expansion to this code? Any help is very much appreciated.

 Accepted Answer

I would use the circshift function with it. Then put it in a loop to do what you want.
For example, this code shifts ‘A’ one row up, so that row #2 becomes row #1 after the shift, and so with the others. Note that the top row wraps to the bottom row with each shift, so you will have to take this into account when you write your code.
A = reshape([1:20],4,[])';
B = circshift(A, [-1 0]);

4 Comments

a={rand(1877958,7); rand(1251972,7)};
b=cellfun(@(x) [x; repmat(x(end,:),-mod(size(x,1),-60),1)],a,'un',0)
for i=1:length(b)
f(i)={transpose(circshift(b{i},[-1 0])).'}
mn(i)={max(reshape(f{i}(:,4),60,[])).'};
mx(i)={min(reshape(f{i}(:,5),60,[])).'};
end
How do I create the loop?
I would put the loop that calculates the shift outside (enclosing) your ‘i’ loop:
a={rand(1877958,7); rand(1251972,7)};
b=cellfun(@(x) [x; repmat(x(end,:),-mod(size(x,1),-60),1)],a,'un',0);
n = 10;
for k = 1:n
for i=1:length(b)
c = circshift(b{i}, [-k 0]);
mn(i,k)={max(reshape(c(:,4),60,[])).'};
mx(i,k)={min(reshape(c(:,5),60,[])).'};
end
end
so now ‘mn’ and ‘mx’ contain an extra dimension, ‘k’, that tracks the shift. Define ‘n’ to be whatever you want it to be. Note that the top row wraps to the bottom row with each shift, so you will have to take this into account when you write your code, and especially to determine your value for ‘n’.
Thanks, but the problem with this formula is that the very first iteration (1 to 60) is not included. One could resolve this problem by changing the line:
for k = 0:n
but that would give me the following error: Subscript indices must either be real positive integers or logicals.
So is there a way to resolve this?
Probably the easiest would be to change the circshift call to:
c = circshift(b{i}, [-(k-1) 0]);
leaving the rest unchanged.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

AA
on 9 Nov 2014

Commented:

on 9 Nov 2014

Community Treasure Hunt

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

Start Hunting!