find combination of labels

조회 수: 2 (최근 30일)
elisa ewin
elisa ewin 2016년 4월 20일
편집: Jos (10584) 2016년 4월 20일
Hello! I describe the view of my problem: I have an user that visit same locations. This location are indicated like
A=[618 574 608];
every value in A indicate a location. To every location correspond different labels that indicate the places that a user can visit in a location:
B=[574 2;574 3;608 1;618 1;618 2;618 3;618 4];
The first column of B indicates the locations and the second column indicates the label associated to every location. E.g. Location 574 corresponds labels 2,3 Location 608 corresponds label 1 Location 618 corresponds label 1,2,3,4
What I want: starting from A and considering B, in which are indicated location and associated labels, I want to find all the possible combination of label of the sequence of location in A. The aspected output is
C=[ 121; 221; 321; 421; 131; 231; 331; 431 ]
Now I explain the output: A=[618 574 608], label associated to 618 are 1,2,3,4: the first digits of values in C; label associated to 574 are 2,3: second digits of values in C; label associated to 608 is 1: third digits of values in C.
Can you help me? I hope the question is clear: if not, I can try to explain better.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 20일
This is not clear
elisa ewin
elisa ewin 2016년 4월 20일
편집: elisa ewin 2016년 4월 20일
what is not clear? the output? I want to find the combination of (1,2,3,4) (2,3) 1

댓글을 달려면 로그인하십시오.

채택된 답변

Ced
Ced 2016년 4월 20일
편집: Ced 2016년 4월 20일
Hi
Your procedure has two steps:
1. find all labels for a current position
2. find all possible combinations
This is one possible way of doing it
A=[618 574 608];
B=[574 2;574 3;608 1;618 1;618 2;618 3;618 4];
% 1. find label for each position
N = length(A);
A_mat = cell(N,1);
for i = 1:N
A_mat{i} = B(B(:,1) == A(i),2);
end
% 2. combine
[ X, Y, Z ] = meshgrid(A_mat{:});
C = str2num([ num2str(X(:)) num2str(Y(:)) num2str(Z(:)) ]);
NOTE: I am assuming that the order of the combinations at the end does not matter? Otherwise, you will need to reorder them
EDIT: If you have more than 3 nodes in A, you can use ndgrid instead of meshgrid. Have a look at Guillaume's answer.
  댓글 수: 2
elisa ewin
elisa ewin 2016년 4월 20일
If the dimension of A are different of 3 (for example 5), meshgrid don't work: how can I replace it?
Guillaume
Guillaume 2016년 4월 20일
See my answer that does not depend on the size of A.
It also uses simple arithmetic to construct your final numbers instead of number to string and string to number conversions so should be much faster.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2016년 4월 20일
편집: Guillaume 2016년 4월 20일
Here is one way to do it. Note that I'm using ndgrid to generate all the combinations. If you have too many locations and labels, ndgrid will run out of memory.
A=[618 574 608];
B=[574 2;574 3;608 1;618 1;618 2;618 3;618 4];
C = arrayfun(@(loc) B(ismember(B(:, 1), A), 2), A, 'UniformOutput', false); %find all labels for each location
[C{:}] = ndgrid(C{:}); %generate all possible combinations
C = cellfun(@(c) c(:), C, 'UniformOutput', false); %reshape into column vectors
C = [C{:}] %and concatenate all into one vector
%I would leave the output C as is, if you really want it as one number:
D = sum(bsxfun(@times, C, 10.^(size(C, 2)-1:-1:0)), 2)
edit: typo in the code
  댓글 수: 1
Ced
Ced 2016년 4월 20일
I believe the C = arrayfun... line should be
C = arrayfun(@(loc) B(ismember(B(:, 1), loc), 2), A, 'UniformOutput', false); %find all labels for each location
i.e. the "A" should be "loc"

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2016년 4월 20일
편집: Jos (10584) 2016년 4월 20일
A simple one-liner will do
A = [618 574 608]
B = [574 2;574 3;608 1;618 1;618 2;618 3;618 4]
C1 = arrayfun(@(k) B(B(:,1)==A(k),2), 1:numel(A), 'un', 0)
The cell C1{i} now holds the values of B(:,2) for which B(:,1) equals A(i) To get all the combinations, my function ALLCOMB (a powerful wrapper around ndgrid) makes that quite easy:
C2 = allcomb(C1{:})
ALL COMB can be downloaded from the Matlab File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/10064
  댓글 수: 2
Guillaume
Guillaume 2016년 4월 20일
I'm unclear how that is different from my:
C = arrayfun(@(loc) B(ismember(B(:, 1), A), 2), A, 'UniformOutput', false)
and how that answer the question of generating all the combinations
Jos (10584)
Jos (10584) 2016년 4월 20일
No need of ismember here. I forgot about the combinations (now added).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by