finding the maximum and minimum value of specific rows in a cell array
5 views (last 30 days)
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
2 Comments
dpb
on 8 Nov 2014
2) is trivial; simply iterate of k
For 1) do you want 1:60 and then 61:120 or 1:60, 2:61, ... ?
Accepted Answer
Azzi Abdelmalek
on 8 Nov 2014
Look at this example
a={rand(1877958,8); rand(1251972,8)};
b=cellfun(@(x) reshape([x; repmat(x(end,:),-mod(size(x,1),-60),1)],60,8,[]),a,'un',0)
out=cellfun(@(x) permute(reshape(min(x),8,[]),[2 1 3]),b,'un',0)
2 Comments
Azzi Abdelmalek
on 8 Nov 2014
a={rand(1877958,8); rand(1251972,8)};
b=cellfun(@(x) reshape([x; repmat(x(end,:),-mod(size(x,1),-60),1)],60,8,[]),a,'un',0)
out=cellfun(@(x) permute(reshape(min(x),8,[]),[2 1 3]),b,'un',0)
min_out5=cellfun(@(x) x(:,5),out,'un',0)
min_out4=cellfun(@(x) x(:,4),out,'un',0)
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
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.
See Also
Categories
Find more on Matrix Indexing 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!