Naseeb Thapaliya
Naseeb Thapaliya

Reputation: 31

Divide a matrix into small matrices with equal number of columns using for loop in Matlab?

I have a matrix with size 1*92609 . I want to loop the matrix and take 8 values at a time to perform element wise multiplication with another 8 bit size matrix. Either, what i want to do is divide the matrix, such that in the first iteration only the first 1:8 elements are taken, and, in second iteration, 8:16 elements are taken and so on.

How can I achieve this in matlab using "for" loop. Here, is the screen shot of the matrix. enter image description here

Upvotes: 0

Views: 208

Answers (2)

shade
shade

Reputation: 161

x is your original array and you want take mean of each 8 elements

n = floor( length(x)/8 );       % how many series of 8 elements in x
y = zeros( 1,n );               % preallocate
k = 0;                          % counter for y array
for i = 1:8:length(x)-1
    k = k + 1;
    y(k) = mean( x(i:i+7) );    % take mean of each 8 elements
end

Upvotes: 0

JAC
JAC

Reputation: 466

If your matrix is MTX you could do something like

N = numel(MTX)
for k = 1:8:N
   subMtx = MTX(k:min(k+7,N));
   do your processing with subMtx
end

The expression 1:8:N gives the sequence 1, 9, 17, .... The min(k+7,N) is necessary because the matrix size (92609) is not divisible by 8. Beware that the last sub-matrix has only one element.

HTH

Upvotes: 1

Related Questions