finding the maximum and minimum value of specific rows in a cell array

4 vues (au cours des 30 derniers jours)
AA
AA le 8 Nov 2014
Commenté : dpb le 9 Nov 2014
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 commentaires
dpb
dpb le 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, ... ?
AA
AA le 8 Nov 2014
2) is solved
1)yes I want exactly that. I want one column with 1:60 then 61:120 and so on

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 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 commentaires
AA
AA le 8 Nov 2014
Modifié(e) : AA le 8 Nov 2014
thanks but I only want column 5 and 6 of each matrix in the cell array
Azzi Abdelmalek
Azzi Abdelmalek le 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)

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 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 commentaires
AA
AA le 9 Nov 2014
Modifié(e) : AA le 9 Nov 2014
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
dpb
dpb le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by