could anyone help me how to group the numbers into sets

조회 수: 2 (최근 30일)
jaah navi
jaah navi 2019년 9월 19일
댓글: dpb 2019년 9월 20일
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
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
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
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)

Fangjun Jiang
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.

thoughtGarden
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
Guillaume
Guillaume 2019년 9월 19일
편집: Guillaume 2019년 9월 19일
A much simpler way to achieve the same:
NN = num2cell(BB)
if that's what the OP is after...
Oh, and
length(something(:, 1))
would be calculated faster with
size(something, 1)
as it doesn't involve creating a temporary matrix. Ditto for other dimensions.
thoughtGarden
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by