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

How to sort an array and divide it to n parts depending of the values in it

Asked by Miguelcm90 on 3 Sep 2013

Hi everybody, First of all , I would like to thank in advance the help.

I have a problem, the first one it is that I am bennigner in matlab and I do not know probably If my explanation is good.

I have the following file:

I know how to open, and manage with it. what I would like to know is as you see, there are 4 columms, each columm has 3 groups of values from min to max I want to make some loop to crop the matrix and divide in 3 diferent matrix that have only one group from min to max, for example,

1 1
2 2
3 3 
1 1 
2 2
3 3
1 1
2 2
3 3 

and obtain 3 diferent matrix like below:

1 1 
2 2 
3 3 

the purpose of using a loop is because the file size changes in rows and columns.

Any help is deeply valuable for me.

Heartfelt thanks,





No products are associated with this question.

1 Answer

Answer by Andrei Bobrov on 3 Sep 2013
Edited by Andrei Bobrov on 3 Sep 2013
m = dlmread('pathtovalidx.txt');
idx = cumsum([true;diff(m(:,1)) < 0]);
a = unique(histc(idx,1:idx(end)));
if numel(a) == 1
    out = reshape(m,a,size(m,2),[]);
    out = mat2cell(m,a,size(m,2));

here out is three dimension double array or cell array, where each page or cell with your new matrix


Andrei Bobrov

Contact us