could anyone help me how to group the numbers into sets
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2019-9-20
编辑:Guillaume
2019-9-20
You're going to have to do your grouping row by row. Neither mat2cell nor num2cell can do that on the whole matrix.
What does not very close to each other and similar actually mean mathematically. Sorry, there is no not_very_close_to_each_other function in matlab, so we need to know how to code that statement.
dpb
2019-9-20
" 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.
回答(3 个)
Guillaume
2019-9-19
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 个评论
Fangjun Jiang
2019-9-19
How do you want to group your sets? See the help or doc of mat2cell. There are examples. You should be able to figure it out.
0 个评论
thoughtGarden
2019-9-19
编辑:thoughtGarden
2019-9-19
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 个评论
thoughtGarden
2019-9-19
I realized the num2cell(BB) solution right after I hit submit. Thanks for pointing out the efficiency of size() over length().
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!