Concatenate two n-dimensional cell arrays into n-by-n-dimensional
You can use allcomb
from MATLAB File-exchange to help you with this -
mat2cell(allcomb(a,a),ones(1,numel(a)^2),2)
source to share
Just for fun, using kron
and repmat
:
a = {1;2;3}
b = mat2cell([kron(cell2mat(a),ones(numel(a),1)) repmat(cell2mat(a),numel(a),1)])
Here, square brackets [] are used to perform the concatenation of both column vectors, where each is defined as either kron
or repmat
.
This can be easily generalized, but I doubt this is the most efficient / quickest solution.
source to share
You can use this meshgrid
to help create unique permutations of pairs of values ββin a
by expanding both matrix outputs meshgrid
so that they fit into the matrix N x 2
. After that, you can determine the final result using mat2cell
to create a 2D cell array. In other words:
a = {1;2;3}; [x,y] = meshgrid([a{:}], [a{:}]); b = mat2cell([x(:) y(:)], ones(numel(a)*numel(a),1), 2);
b
will contain your 2D cell array. To understand what is happening at each step, it looks like what the output of the second line looks like. x
and y
are actually 2D matrices, but I'm going to expand them and show that they are both in a matrix, where I have combined both together:
>> disp([x(:) y(:)])
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Concatenating both vectors together into a 2D matrix is ββimportant for the next line of code. This is an important step to achieve what you want. After the second line of code, the goal is to make each element of this concatenated matrix into a separate cell in the cell array, which it does mat2cell
at the end. By running this last line of code, then displaying the content b
, this is what we get:
>> format compact;
>> celldisp(b)
b{1} =
1 1
b{2} =
1 2
b{3} =
1 3
b{4} =
2 1
b{5} =
2 2
b{6} =
2 3
b{7} =
3 1
b{8} =
3 2
b{9} =
3 3
b
will be a 9 element cell array and inside each cell there will be another cell array 1 x 2
that stores one row of the concatenated matrix as separate cells.
source to share