# could anyone help me how to group the numbers into sets

3 views (last 30 days)

Show older comments

BB =[1 2 3 4 5 6;

1 3 2 4 5 6;

1 4 2 3 5 6;

2 4 1 3 5 6;

1 5 2 3 4 6;

4 5 1 2 3 6;

1 6 2 3 4 5;

5 6 1 2 3 4]

i tried with the following command

NN=mat2cell(BB,[1,1,1],2)

but i am getting error using mat2cell Input arguments, D1 through D2, must sum to each dimension of the

input matrix size, [8 6].'

Could anyone please help me on this.

##### 4 Comments

dpb
on 20 Sep 2019

" there is no not_very_close_to_each_other function in matlab"

ismembertol() might be of some help.

But, it's certainly not obvious from the input and description why the desired output is what it is.

### Answers (3)

Guillaume
on 19 Sep 2019

As the documentation of mat2cell explain, the dim inputs specify the size of each block along a particular dimension. The sum of the size of the blocks must equal the size of the matrix to be split in each dimension. So, with

mat2cell(A, dim1, dim2)

sum(dim1) must equal size(A, 1) and sum(dim2) must equal size(A, 2).

You haven't properly explained what you're trying to do, so we can't really tell you what the solution is. One valid use of mat2cell with your input matrix would be:

mat2cell(BB, [3 2 1 2], [4 1 1])

since the first dim argument sums to 8 (creates blocks of height 3, 2, 1, and 2 respectively) which is the number of rows of BB, and the second dim sums to 6, the number of columns of BB.

Now, perhaps you want to split the matrix into 1x1 cells, in which case the proper function is num2cell not mat2cell:

num2cell(BB)

Or you want to split it into a cell array of row vectors:

num2cell(BB, 1)

or column vectors

num2cell(BB, 2)

##### 0 Comments

Fangjun Jiang
on 19 Sep 2019

##### 0 Comments

thoughtGarden
on 19 Sep 2019

Edited: thoughtGarden
on 19 Sep 2019

Its not clear what you are trying to accomplish. If you want to create a cell array where each matrix value is a cell, then the following would work:

BB =[1 2 3 4 5 6;

1 3 2 4 5 6;

1 4 2 3 5 6;

2 4 1 3 5 6;

1 5 2 3 4 6;

4 5 1 2 3 6;

1 6 2 3 4 5;

5 6 1 2 3 4]

NN=mat2cell(BB,[ones(1,length(BB(:,1)))],[ones(1,length(BB(1,:)))])

% or betterl

NN = num2cell(BB);

The error arrises because, as it states, the sum of the values of the input arguments must equal the dimensions of the matrix.

The result is this:

NN =

8×6 cell array

{[1]} {[2]} {[3]} {[4]} {[5]} {[6]}

{[1]} {[3]} {[2]} {[4]} {[5]} {[6]}

{[1]} {[4]} {[2]} {[3]} {[5]} {[6]}

{[2]} {[4]} {[1]} {[3]} {[5]} {[6]}

{[1]} {[5]} {[2]} {[3]} {[4]} {[6]}

{[4]} {[5]} {[1]} {[2]} {[3]} {[6]}

{[1]} {[6]} {[2]} {[3]} {[4]} {[5]}

{[5]} {[6]} {[1]} {[2]} {[3]} {[4]}

However if you want to break up the matrix into multiple cells (say two vertical and two horizontal) than do the following...

BB =[1 2 3 4 5 6;

1 3 2 4 5 6;

1 4 2 3 5 6;

2 4 1 3 5 6;

1 5 2 3 4 6;

4 5 1 2 3 6;

1 6 2 3 4 5;

5 6 1 2 3 4];

NN = mat2cell(BB,[4,4],[3,3]);

which results in

NN =

2×2 cell array

{4×3 double} {4×3 double}

{4×3 double} {4×3 double}

##### 2 Comments

thoughtGarden
on 19 Sep 2019

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!