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

# 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:

https://www.dropbox.com/s/1wav4fb1afqx8ix/validx.txt

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,

Miguel

## Products

No products are associated with this question.

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),[]);
else
out = mat2cell(m,a,size(m,2));
end
```

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