Get all sets of cells from two arrays of cells, each of which contains exactly one cell from each column of each arrays.

1 visualizzazione (ultimi 30 giorni)
There are two arrays of cells: A and B. They have a different size (size A is 7 by n, size B is 3 by m).
It is nessesary to form all sets of cells, each of which contains exactly one cell from each column of A and one cell from each column of B.
Thus, in each set we have n+m cells.
The total quantity of sets is: 7^n*3^m.
But how to get them?
  2 Commenti
Guillaume
Guillaume il 17 Giu 2019
I'm assuming that your arrays of cells are actually matrices and not cell arrays.
I don't understand your description on how the two matrices are to be combined. Can you give a concrete example? e.g. given
A = reshape(1:14, 7, 2); %A 7 x 2 matrix
B = reshape(1:12, 3, 4); %A 3 x 3 matrix
what is the desired result (using matlab syntax)?
VALERIY ZLOBIN
VALERIY ZLOBIN il 17 Giu 2019
Modificato: VALERIY ZLOBIN il 17 Giu 2019
1. I am talking about an array of cells.
2. In your example, matrix B has a size of 3 by 4, but not 3 by 3).
3. The result for your example is the matrix C, which has a size of 6 by 3969. Each column of the matrix C is the required set. For example, the first column of matrix C may contain all the elements of the first row of matrix A and all the elements of the first row of matrix B; The second column of matrix C can contain all elements of the first row of matrix A, the first, second and third elements of the first row of matrix B, and the fours element of the second row of matrix B.
4. Let me know if the task is clear now, please.
P.S.
C=
1 1 1 ...
8 8 8 ...
1 1 1 ...
4 4 4 ...
7 7 7 ...
10 11 12 ...

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 17 Giu 2019
There's no such thing as an array of cells in matlab. There are matrices or arrays (a matrix being a 2D array) and there are cell arrays (where each cell can contain another array).
First, a helper function:
function combs = allcomb(maxval, permlength)
%create all permutations of length permlength of numbers from 1 to maxval
combs = cell(1, permlength);
[combs{:}] = ndgrid(1:maxval);
combs = reshape(cat(permlength + 1, combs{:}), [], permlength);
end
Then:
%some demo data
A = reshape(1:14, 7, 2); %A 7 x 2 matrix
B = reshape(1:12, 3, 4); %A 3 x 3 matrix
rowsA = allcomb(size(A, 1), size(A, 2));
rowsB = allcomb(size(B, 1), size(B, 2));
colsA = repmat(1:size(A, 2), size(rowsA, 1), 1);
colsB = repmat(1:size(B, 2), size(rowsB, 1), 1);
indA = sub2ind(size(A), rowsA, colsA);
indB = sub2ind(size(B), rowsB, colsB);
[indArow, indBrow] = ndgrid(1:size(indA, 1), 1:size(indB, 1));
C = [A(indA(indArow(:), :)), B(indB(indBrow(:), :))]
Note that the above works with both matrices and 2D cell arrays.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by