DIVIDE a mxn MATRIX in c size new matrix

1 view (last 30 days)
Miguelcm90
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
dpb
dpb 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
results=yourfunction(x(i1:i2,:));
% do whatever with those results here
i1=i2+1; i2=i2+c; % next group indices
end
...
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.

Sign in to comment.

Answers (1)

Matt J
Matt J on 3 Sep 2013
You could use MAT2TILES ( Available Here ).
Example:
m=12; n=10; c=3;
A=rand(m,n);
C=mat2tiles(A,[m/c,inf])
leads to three matrices C{1}, C{2}, C{3} that are 4x10,
C =
[4x10 double]
[4x10 double]
[4x10 double]

Community Treasure Hunt

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

Start Hunting!