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

조회 수: 1 (최근 30일)
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
Stephen23
Stephen23 2016년 8월 1일
편집: Stephen23 2016년 8월 2일
@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:

댓글을 달려면 로그인하십시오.

답변 (2개)

Geoff Hayes
Geoff Hayes 2016년 8월 1일
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
gooniyath
gooniyath 2016년 8월 1일
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 2016년 8월 2일
@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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 8월 2일
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)

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by