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

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 comentarios

2) is trivial; simply iterate of k
For 1) do you want 1:60 and then 61:120 or 1:60, 2:61, ... ?
2) is solved
1)yes I want exactly that. I want one column with 1:60 then 61:120 and so on

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentarios

AA
AA el 8 de Nov. de 2014
Editada: AA el 8 de Nov. de 2014
thanks but I only want column 5 and 6 of each matrix in the cell array
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)

Iniciar sesión para comentar.

Más respuestas (1)

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 comentarios

ok i will try but it is a lot of work to delete the rows for every cell array
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.
AA
AA el 9 de Nov. de 2014
Editada: AA el 9 de Nov. de 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
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.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

AA
el 8 de Nov. de 2014

Comentada:

dpb
el 9 de Nov. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by