Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to find the maximum after every 5 rows in a matrix?

1 回表示 (過去 30 日間)
B.kun
B.kun 2014 年 7 月 1 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi all, I have a matrix of size 525600x23 and I would like to find the maximum value after every 5 rows, so that my output matrix will have the size 105120x23.
I used the following code (because it worked with finding the Mean) but the output dimension for this case gave me 5x105120x23. However I wish to actually get 105120x23 size only.
Answer = squeeze(nanmax(reshape(Mymatrix,[5,105120,23]),1));
Any help is appreciated.
Thank you!

回答 (1 件)

dpb
dpb 2014 年 7 月 1 日
編集済み: dpb 2014 年 7 月 1 日
nGrp=5; % grouping size
mx=reshape(nanmax(reshape(x,nGrp,[])),size(x,1)/nGrp,[]);
The key is to remember storage order is column major. Try the following at the command line to visualize...
x=rand(8,3);
nGrp=2;
Now apply the above and look at x and mx. If you still don't follow the reason it works, break down the expression and do the two pieces individually looking at the result of each step.
  3 件のコメント
B.kun
B.kun 2014 年 7 月 1 日
Ah, no worries! I reshaped your code once again and I got what I wanted.
mmx = reshape(mx,105120,23);
Thank you.
dpb
dpb 2014 年 7 月 1 日
The key to these kinds of problems is to work on small subsets you can visualize at the command line...
Sorry, I was typing at the command line and missed the divisor to get the number of rows divided by the grouping constant...and was thinking of the transposed x instead of original, too. Edited Answer as well.
mx=reshape(nanmax(reshape(x,nGrp,[])),size(x,1)/nGrp,[]);

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by