Asked by mohamed Faraj
on 15 Aug 2019

I have a cell array with a variable number of cells and a variable number of elements within each cell. For instance, it could by V{1}{1}=1 2 3 4, V{1}{2}=3 4 1 0,V{1}{3}=3 5 -1 -9, and V{2}{1}=-1 -2 9, V{2}{2}= 0 9 6, i.e., the cell 1 has 3 vectors of length 4 elements and the cell 2 has 2 vectors of length 3. I want to construct another cell array such that it has all combinations of these vectors, here I could have 6 combinations (3*2=6). The new cell array has to be of length 6 cells (number of possible combinations), and each cell has a possible combinations, for example A{1}={1 2 3 4 and -1 -2 9} and A{2}={1 2 3 4 and 0 9 6} . Note that I will get these vectors from a matlab code so I do not know them in advance.

Answer by Adam Danz
on 15 Aug 2019

Edited by Adam Danz
on 17 Aug 2019

Accepted Answer

This fits the description from your comment under your question. It horizontally concatenates all combinations of the row vectors stored in V{1} and V{2}.

% Demo data

V{1}{1} = 1:4; % vec1 1x4

V{1}{2} = 2:5; % vec2 1x4

V{1}{3} = 3:6; % vec3 1x4

V{2}{1} = 1:3; % vec4 1x3

V{2}{2} = 2:4; % vec5 1x3

[v1idx, v2idx] = ndgrid(1:numel(V{1}), 1:numel(V{2}));

D = cellfun(@(a,b)horzcat(a,b),{V{1}{v1idx(:)}}, {V{2}{v2idx(:)}}, 'UniformOutput', false)

% D = {[vec1,vec4], [vec2,vec4], [vec3,vec4], [vec1,vec5], [vec2,vec5], [vec3,vec5]}

Version 2: when number of sub-cells in V is variable.

cellCount = num2cell(1:numel(V));

nCellIdx = cellfun(@(x)1:numel(x), V, 'UniformOutput', false);

cellGrid = cell(size(nCellIdx));

[cellGrid{:}] = ndgrid(nCellIdx{:});

matGrid = cell2mat(cellfun(@(x)x(:),cellGrid,'UniformOutput',false)); % Each row is the index for 1 cell

D = cell(1,size(matGrid,1));

for i = 1:size(matGrid,1)

D{i} = cellfun(@(ci,sci)V{ci}(sci),cellCount, num2cell(matGrid(i,:)));

end

D(i) contains the sub-cell indices listed in matGrid(i,:).

mohamed Faraj
on 16 Aug 2019

Below is information about my cell array. Its name is "allPaths_service"

>> allPaths_service

allPaths_service =

{5x1 cell} {3x1 cell} {3x1 cell}

********************************************

>> size(allPaths_service)

ans =

1 3

********************************************

>> allPaths_service{:}

ans =

[1x6 double]

[1x6 double]

[1x6 double]

[1x6 double]

[1x6 double]

ans =

[1x5 double]

[1x5 double]

[1x5 double]

ans =

[1x5 double]

[1x5 double]

[1x5 double]

********************************************

>> allPaths_service{1}{:}

ans =

1 2 3 4 5 10

ans =

1 2 3 4 9 10

ans =

1 6 7 4 5 10

ans =

1 6 7 4 9 10

ans =

1 6 7 8 9 10

********************************************

>> allPaths_service{2}{:}

ans =

1 2 3 5 10

ans =

1 6 7 5 10

ans =

1 6 7 9 10

********************************************

>> allPaths_service{3}{:}

ans =

1 2 3 5 10

ans =

1 6 7 5 10

ans =

1 6 7 9 10

********************************************

Please note that this array is generated from another matlab code, so it may change size

Adam Danz
on 16 Aug 2019

Could you save "allPaths_service" to a mat file and attach it?

Also, could you provide a small example of the expected outcome (using real data). I know it would take time to manually produce the entire output but a few combinations would be helpful. For example, it's still unclear how a 1xn vector will be combined with a 1xm vector.

Adam Danz
on 17 Aug 2019

Sign in to comment.

Answer by mohamed Faraj
on 17 Aug 2019

mohamed Faraj
on 19 Aug 2019

Thank you Adam. Thi s was very helpful. One last thing. I noticed that t

matGrid =

1 1

2 1

3 1

1 2

2 2

3 2

what if I want ti to be in this order

matGrid =

1 1

1 2

2 1

2 2

3 1

3 2

which means we first change the index of the right most column first.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Adam Danz (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476307-how-to-construct-a-cell-array-from-another-cell-array#comment_735509

## mohamed Faraj (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476307-how-to-construct-a-cell-array-from-another-cell-array#comment_735515

## Adam Danz (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476307-how-to-construct-a-cell-array-from-another-cell-array#comment_735520

Sign in to comment.