HOW TO GROUP THE ELEMENTS OF AN ARRAY

4 ビュー (過去 30 日間)
simone zappalà
simone zappalà 2022 年 5 月 10 日
回答済み: Image Analyst 2022 年 5 月 10 日
I've a 252*51 array, i want to group it three-by-three elements and do mean of these groups one-by-one, creating a new array 84*51 which every row is the mean of three elements of the first array

採用された回答

dpb
dpb 2022 年 5 月 10 日
One of those cases where the optimal way depends upon being familiar with internal array storage order being column-major and manipulation of orientation/shape to take advantage of vector operations --
nRowToAvg=3;
mnAbynRowToAvg=reshape(mean(reshape(A,nRowToAvg,[])),[],size(A,2));

その他の回答 (2 件)

Prakash S R
Prakash S R 2022 年 5 月 10 日
編集済み: Prakash S R 2022 年 5 月 10 日
A more "arithmetic" approach:
So you want to take a 252x51 matrix X and produce a 84x51 matrix Y, right? I would suggest premultiplying X with a 84x252 matrix A that sums successive rows with weights of 1/3.
Thus, Y = (1/3)*A*X, where A has the form
[1 1 1 0 0 0 0 0 0 0 0....
0 0 0 1 1 1 0 0 0 0 0...
0 0 0 0 0 0 1 1 1 0 0 ...
:
0 0 0 ... 1 1 1]
You can construct A using
aa = [[1 1 1], zeros(1,249)];
Atmp = triu(toeplitz(aa));
A = Atmp(1:3:end, :);

Image Analyst
Image Analyst 2022 年 5 月 10 日
One way is to use blockproc(). It's in the Image Processing Library.
meanFilterFunction = @(theBlockStructure) mean(theBlockStructure.data(:)); % Define mean function.
blockSize = [3, 3]; % Whatever window size you want.
% Get an output image where each pixel is the average of a 3x3 window in
% the original image.
blockyImage = blockproc(grayImage, blockSize, meanFilterFunction);
I'm attaching a script with several ways to use it. Adapt as needed.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by