I have a matrix with 5 columns and one million rows. How can I find the max value of every 60 rows in that rows of million

 채택된 답변

Jan
Jan 2014년 10월 12일

0 개 추천

Reshape the array to a 3D array with 60 elements in the first dimension. Then find the maximum. The most work is required to consider that the 1st dimension is not a multiple of 60:
data = rand(1e6, 5);
siz = size(data);
s1 = siz(1) - mod(siz(1), 6);
x = reshape(data(1:s1, :), 60, s1/60, siz(2));
result = max(x, [], 1);

댓글 수: 2

Matt J
Matt J 2014년 10월 12일
편집: Matt J 2014년 10월 12일
Yep, that's the thing to do. I happily accept this on AA's behalf :D
Matt J
Matt J 2014년 10월 13일
In this FEX submission,
I generalized this to N-dimensions and to other operations with similar separability properties to max().

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2014년 9월 28일
편집: Guillaume 2014년 9월 28일

0 개 추천

I would split the matrix into a cell array of 60 rows with mat2cell and then use cellfun (or a loop) to find the max of each cell:
numsplit = ceil(size(m, 1) / 60); %how many cells are needed
rowdist = ones(1, numsplit) * 60; %define the row split
rowdist(end) = mod(size(m, 1), 60); %adjust last distance to be the modulo of row by 60
c = mat2cell(m, rowdist, size(m, 2)); %create a cell array where each cell is 60 rows
if isempty(c{end}) %will happen when size(m, 1) is a multiple of 60
c(end) = [];
end
mx = cellfun(@(mm) max(mm(:)), c);

댓글 수: 4

Matt J
Matt J 2014년 9월 28일
편집: Matt J 2014년 9월 28일
MAT2TILES ( Download ) will take care of a lot of this book-keeping for you,
mx = cellfun(@(mm) max(mm(:)), mat2tiles(m,60,1));
Guillaume
Guillaume 2014년 9월 28일
편집: Guillaume 2014년 9월 28일
It probably should read:
mx = cellfun(@(mm) max(mm(:)), mat2tiles(m,60,Inf));
If that's what the OP intended, then yes. You could also do
mx = cellfun(@(mm) max(mm(:)), mat2tiles(m,60) );
Jan
Jan 2014년 10월 12일
You do not need the indirection over a cell. A 3D array is enough and much faster.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

AA
2014년 9월 28일

댓글:

2014년 10월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by