MATLAB: Find combination of labels

combinationlabels

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.

Best Answer

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.