Split an array into parts in MATLAB

I wanted to split the array into equal parts as follows:

 a=[1 2 3 4 5 6 7 8 9 10]
 n = 2;
 b = split(a, n);

 b =

 1     2     3     4     5
 6     7     8     9    10

What function can do this?

+5
source share
2 answers

Try the following:

a = [1 2 3 4 5 6]
reshape (a, 2, 3)
+13
source

If ayou can divide by n, you can actually only provide one RESHAPE argument.

To change the form into 2 lines:

b = reshape(a,2,[])

To change the form into 2 columns:

b = reshape(a,[],2)

Note that changing the form is done in columns, it fills the first column first, then the 2nd and so on. To get the desired result, you must convert it to 2 columns and then transfer the result.

b = reshape(a,[],2)'

You can put a check before changing:

assert(mod(numel(a),n)==0,'a does not divide to n')
+11
source

All Articles