Split 4000x10 Matrix into 400 10x10 Matrices and calculate stats for each matrix?

1 view (last 30 days)
Hi
Im trying to split a 4000x10 Matrix into 400 10x10 Matrices? Once the matrix is split up I need to calcualte the mean, median and standard deviation for each of these matrices.
Regards Akshay
  1 Comment
Stephen23
Stephen23 on 1 Aug 2016
Edited: Stephen23 on 2 Aug 2016
@Akshay J: learn to use the dimensions of arrays (see Geoff Hayes' answer) or cell arrays. Do not try to create 400 individual matrices. Read this to know why magically creating lots of variables is a bad way to write code:

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 1 Aug 2016
Akshay - try using reshape to split your 4000x10 matrix into a 10x10x400 matrix. For example,
A = randi(255,4000,10);
B = reshape(A,10,10,[]);
Then you can iterate over each of the 400 matrices to calculate your statistics
for k=1:size(B,3)
mtx = B(:,:,k); % the kth 10x10 matrix
% calculate the mean and standard deviation
end
  2 Comments
gooniyath
gooniyath on 1 Aug 2016
Hi Geoff thanks for the help. How do I get it to split up the the data row by row instead of column by column? Cheers
Stephen23
Stephen23 on 2 Aug 2016
@Akshay J: use permute beforehand to swap the columns and rows:
B = permute(A,[2,1,3]);
B = reshape(B,...)
you might want to use permute again after that too.

Sign in to comment.


Image Analyst
Image Analyst on 2 Aug 2016
Akshay, you can use blockproc to process a block of your array (or image) by moving the block, which can be of any size, in "jumps" that you specify. So you have a 4000 row by 10 column matrix. You can move in jumps of 10 rows and then compute the functions you want. Here is the code (requires the Image Processing Toolbox for the blockproc() function):
% Create sample data.
yourMatrix = randi(9, 4000, 10);
% Define how we will move in jumps over the array.
blockSize = [10, 1];
% Define the function that we will apply to each block.
% First in this demo we will take the median value in the block
% and create an equal size block where all elements have the median value.
medianFilterFunction = @(theBlockStructure) median(theBlockStructure.data(:));
% Block process the array to replace every pixel in the
% 10 pixel by 10 pixel block by the median of the pixels in the block.
blockyImageMedian = blockproc(single(yourMatrix), blockSize, medianFilterFunction); % Works.
[rows, columns] = size(blockyImageMedian)
% Block process the image to replace every pixel in the
% 10 pixel by 10 pixel block by the mean of the pixels in the block.
% The matrix is 4000 rows by 10 pixels across which will give 4000/10 = 400 blocks.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:));
blockyImageMean = blockproc(yourMatrix, blockSize, meanFilterFunction);
[rows, columns] = size(blockyImageMean)
% Block process the image to replace every pixel in the
% 10 pixel by 10 pixel block by the standard deviation of the pixels in the block.
StDevFilterFunction = @(theBlockStructure) std(double(theBlockStructure.data(:)));
blockyImageSD = blockproc(yourMatrix, blockSize, StDevFilterFunction);
[rows, columns] = size(blockyImageSD)

Community Treasure Hunt

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

Start Hunting!