MATLAB Answers


DIVIDE a mxn MATRIX in c size new matrix

Asked by Miguelcm90
on 3 Sep 2013

Hi everybody, I would like to divide a matrix, in three smaller matrix with the same number of n columns but rows are m/c, being c the number of parts I want to divide this matrix. So, taking in consideration I use this file,

I would like to mke a loop with condition so as to divide and save this new matrix in a new file or variable to work with it more easily.

I would load the file.dat and read the matrix, run a for loop and read al values, and save them in new three matrix. I am sorry because I am starting to use MATLAB:

Thanks in advance

Best Regards,

  1 Comment

on 3 Sep 2013

Why not simply read the array and then operate on the pieces desired?

If you set the number to look at a time either as a user input or a constant, then a function could be called from a loop using something like

x=importdata('yourfile');   % load the file into array 'x'
c=input('Enter number rows to use for analysis: ')
nr=size(x,1);               % total number rows in x
ng=floor(nr/c);             % number of groups of c in nr
i1=1; i2=nr;                % first group row indices
for idx=1:ng
  % do whatever with those results here
  i1=i2+1; i2=i2+c;         % next group indices

There are more sophisticated ways in Matlab you'll learn w/ time but that's the essence of how to work with arrays in Matlab -- don't start creating new variables willy-nilly; work with the array indices and sub-arrays and the like is much more productive.


No products are associated with this question.

1 Answer

Answer by Matt J
on 3 Sep 2013

You could use MAT2TILES ( Available Here ).


 m=12; n=10; c=3;

leads to three matrices C{1}, C{2}, C{3} that are 4x10,

    C = 
        [4x10 double]
        [4x10 double]
        [4x10 double]


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!