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

4 ビュー (過去 30 日間)
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 件のコメント
Walter Roberson
Walter Roberson 2024 年 8 月 26 日
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 2024 年 8 月 26 日
編集済み: Matt J 2024 年 8 月 26 日
The matrix A have negative and positive decimals, so blockproc() didn't work for me.
Whatever went wrong, that wasn't the reason.

サインインしてコメントする。

採用された回答

Sahas
Sahas 2024 年 8 月 27 日
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!

その他の回答 (1 件)

Matt J
Matt J 2024 年 8 月 26 日
編集済み: Matt J 2024 年 8 月 26 日

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by