MATLAB Answers

0

could anyone help me how to group the numbers into sets

jaah navi 님이 질문을 제출함. 19 Sep 2019
최근 활동 dpb 님이 댓글을 추가함. 20 Sep 2019
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

표시 이전 댓글 수: 1
I am having totally 6 users in my system randomly located based on the coordinates of PPP.
PPP=[ 0.6785 2.7764
-0.8215 0.4474
1.6875 0.8120
2.8651 0.5817
1.8722 2.3299
1.6140 0.8852]
A=[1 2 3 4 5 6;
1 3 2 4 5 6;
2 3 1 4 5 6;
1 4 2 3 5 6;
2 4 1 3 5 6;
2 5 1 3 4 6;
1 6 2 3 4 5;
2 6 1 3 4 5]
with respect to the above matrix the first two values in each row represents the grouping of users in a manner that they are not very close to each other. The rest of the value s in each row needs to check whether the next two values are similar to any of the first two values in any of the row,if yes then those two users needs to be grouped or else the next three users in each row needs to be grouped.Finally the last user can be left alone.
based on the description i need to have the result in the following manner
{1 2} {3 4 5} {6}
{1 3} {2 4} {5} {6}
{2 3} {1 4} {5} {6}
{1 4} {2 3} {5} {6}
{2 4} {1 3} {5} {6}
{2 5} {1 3} {4} {6}
{1 6} {2 3} {4} {5}
{2 6} {1 3} {4} {5}
Could you please help me on this.
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.
" 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.

로그인 to comment.

태그

답변 수: 3

Guillaume 님의 답변 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

로그인 to comment.


Fangjun Jiang 님의 답변 19 Sep 2019

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

로그인 to comment.


thoughtGarden 님의 답변 19 Sep 2019
thoughtGarden 님이 편집함. 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

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.
I realized the num2cell(BB) solution right after I hit submit. Thanks for pointing out the efficiency of size() over length().

로그인 to comment.



Translated by