Clear Filters
Clear Filters

Binning matrix data (negative and positive decimals): mean of each 100 rows and create a new matrix

4 views (last 30 days)
I have a A= 7382300X12 matrix. I want to reduce it by averaging every 1000 rows in all columns. So at the end I will have a B= 73823X12 matrix.
The matrix A have negative and positive decimals, so blockproc() didn't work for me.
How can I do it?
  2 Comments
Walter Roberson
Walter Roberson on 26 Aug 2024
You have the small issue that 7382300 is not evenly divisible by 1000. Should the final 300 entries be treated as a complete group?
Matt J
Matt J on 26 Aug 2024
Edited: Matt J on 26 Aug 2024
The matrix A have negative and positive decimals, so blockproc() didn't work for me.
Whatever went wrong, that wasn't the reason.

Sign in to comment.

Accepted Answer

Sahas
Sahas on 27 Aug 2024
As per my understanding, you would like to reduce the size of an input matrix by taking the mean of every 1000 rows and all the columns.
The following code snippet achieves the desired functionality. You can modify the input matrix to suit your preferred data types.
inputMatrix = randi([-100, 100], 3300, 12);
numRows = size(inputMatrix, 1);
numCols = size(inputMatrix, 2);
blockSize = 1000;
% Calculate the number of complete blocks
numBlocks = floor(numRows / blockSize);
% Reshape inputMatrix to group every 'blockSize' rows together for each column
reshapedA = reshape(inputMatrix(1:blockSize*numBlocks, :), blockSize, numBlocks, numCols);
% Calculate the mean along the first dimension
semiFinalMatrix = squeeze(mean(reshapedA, 1))
semiFinalMatrix = 3x12
-0.1360 1.0220 -1.5510 -0.2930 -1.3370 0.6940 2.4430 0.1380 -5.4680 -2.2770 -2.1510 0.8750 -0.2440 0.1370 1.8730 -1.8800 -1.3020 0.7470 -1.5090 -0.5750 -1.6570 -3.6780 1.0780 -0.3620 0.7000 -0.9950 2.4580 -0.5420 2.3390 1.2080 -4.0240 0.7980 -0.9520 1.9370 2.7380 1.2630
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Assuming treating the leftover rows as one group and getting their mean as well
remainingRows = mod(numRows, blockSize);
if remainingRows > 0
% Calculate the mean of the remaining rows
remainingMean = mean(inputMatrix(end-remainingRows+1:end, :), 1);
% Append the mean of the remaining rows to semiFinalMatrix
finalMatrix = [semiFinalMatrix; remainingMean]
end
finalMatrix = 4x12
-0.1360 1.0220 -1.5510 -0.2930 -1.3370 0.6940 2.4430 0.1380 -5.4680 -2.2770 -2.1510 0.8750 -0.2440 0.1370 1.8730 -1.8800 -1.3020 0.7470 -1.5090 -0.5750 -1.6570 -3.6780 1.0780 -0.3620 0.7000 -0.9950 2.4580 -0.5420 2.3390 1.2080 -4.0240 0.7980 -0.9520 1.9370 2.7380 1.2630 2.6933 -2.9067 5.5567 -1.9933 -2.2433 3.5667 -1.0933 -6.3200 0.4633 4.0300 0.3400 0.2767
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Refer to the following MathWorks documentation links for more information on “reshape” and “squeeze” functions.
Hope this is beneficial!

More Answers (1)

Matt J
Matt J on 26 Aug 2024
Edited: Matt J on 26 Aug 2024

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!