Applying similar functions to many small chunks of large matrices

1 view (last 30 days)
I have a large matrix and want to apply similar functions across many smaller portions of the matrix. The matrix I am working with has billions of datapoints, so I am building a smaller replica to practice with.
An example of the practice matrix would be...
T = 5;
W = zeros(10,5);
for trial = 1:T
mtest = randi([1,5],10,3);
W(:,trial) = var(mtest,0,2);
end
M = W(:)
lindex = 0:0.2:1.8;
linc = zeros(50,10);
for i = 1:length(lindex)
linc(:,i) = M >= lindex(i);
end
So, you can see this is a 50X10 matrix of 0's and 1's. Each subset that I want to analyze is a 2X1 chunk of the 50X10 matrix (e.g., linc(1:2,1), linc(1:2,2), linc(1:2,3)...all the way to linc(49:50,10)). If I want to do something simple like calculate the percentage of 1's in each subset, that would require 250 calculations. Is there a function I can write to perform such a calculation in one or a few steps?
I have considered for loops, while loops, and splitapply, but none of them seem appropriate here, though I may be wrong.
Any suggestions?
Thank you in advance!

Accepted Answer

Ameer Hamza
Ameer Hamza on 13 Mar 2020
See blockproc: https://www.mathworks.com/help/images/ref/blockproc.html. You can apply same function on smaller blocks of a matrix. For example
M = rand(6) > 0.5; % random binary matrix
blockproc(M, [1 2], @(x) sum(x.data==1))
This statement will count the number of ones in every 1x2 block of matrix M.
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!