finding the maximum and minimum value of specific rows in a cell array
Show older comments
I have a cell array (1x99) which consists of the following matrices:
1877958x8 double
1251972x8 double
938979x8 double
751183x8 double
625986x8 double
536560x8 double
469490x8 double
417324x8 double
375592x8 double
341447x8 double
and so on.
For each of these matrices, I want to get the maximum and minimum value of 60 rows. I created the following formula:
minCol5 = min(myCells{k}(1:60,5));
maxCol4 = max(myCells{k}(1:60,4));
There are two problems with this formula:
1) it does not give me the minimum/maximum of the next 60 consecutive rows until all rows are covered.
2) it is only applies to the very first matrix and does not cover the rest of the matrices in the cell array.
How can I resolve this issue?
Thanks
Accepted Answer
More Answers (1)
dpb
on 8 Nov 2014
The simplest way to do the averaging over a fixed block size is to reshape by that number, apply the operation and then reshape back.
However, you've got a problem -- you do realize that none of the sizes you list above are evenly divisible by 60, I presume? W/O that complication, for each array it's simply
for i=1:length(A)
mn(i)={min(reshape(A{i}(:,5),60,[])).'};
mx(i)={max(reshape(A{i}(:,6),60,[])).'};
end
You'll have to fixup the length of each subarray to be a multiple of 60 so the reshape above works and then either ignore the odd rows or compute the last short set after the above.
4 Comments
AA
on 8 Nov 2014
dpb
on 8 Nov 2014
Well, you've got to do something as you can't average by 60 when mod(N,60)~=0 for all groups.
It's certainly not hard to compute the last multiple of 60 index for each array and only select that number of rows.
dpb
on 9 Nov 2014
I'd have just selected the length within the loop for each array...
for i=1:length(A)
L=fix(length(A{i)}/60);
mn(i)={min(reshape(A{i}(1:L,5),60,[])).'};
mx(i)={max(reshape(A{i}(1:L,6),60,[])).'};
end
You can write the expression for L in the subscripting but since it's used twice saves one operation at the expense of the temporary.
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!