Cartesian product of 2 matrices

Let A and B are sets of 16 matrices size 2x2, say A = {M1,M2,M3,...,M16}, B = {M1,M2,M3,...,M16}, with entry each Mi, i = 1,..,16 are 0 and 1, eg : M1=[0 0; 0 0], M2 = [0 1; 0 0],...,M16 = [1 1; 1 1]. How the syntax of those cartesian product of A and B ? Later the result AxB = {(M1,M1), (M1,M2),...,(M16,M16)} with total elements of AxB are 256 paired orders.

1 Commento

May i ask what was the motivation for this problem. I what type of problem did you find such cartesian product of matrices

Accedi per commentare.

Risposte (1)

Torsten
Torsten il 8 Giu 2022
Modificato: Torsten il 8 Giu 2022
m = 4;
justRows = dec2bin(0:2^m-1)-'0';
n = size(justRows,1);
M = cell(2^m,1);
for i = 1:n
A = reshape(justRows(i,:),[sqrt(m),sqrt(m)]);
M{i} = A;
end
McrossM = cell(n,n);
for i=1:n
for j=1:n
McrossM{i,j} = {M{i},M{j}};
%disp(McrossM{i,j})
end
end

2 Commenti

thankyou sir for the answer, but i mean for this case how to make tuple (M{i},M{j}) not M{i}*M{j}, then the result is a set of tuples like
A={1 2 3} and B = {a b} then AxB = {(1,a),(2,a),(3,a),(1,b),(2,b),(3,b)};
sign 'cross' means cartesian product not 'multiply' like 2x3=6.
I changed the code accordingly.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by