Search code examples
matlabmatrixcombinationslinear-algebraprobability-theory

MATLAB -- How to create a specific number of combinations of a matrix?


I've created a random 10x10 matrix and fixed it. Now, I want to create, say, 1 million different combinations of this matrix (not all of them). I've emphasized the word "different" because the original random matrix only consists of 4 different elements. Is there a way to do this in MATLAB?


Solution

  • I can't recall from my commutative algebra course if there is a more elegant way to do it. Nevertheless, here is the brute-force method:

    A = vec2mat(randsample(4,100,true),10);%I chose the four elements to be 1,2,3,4
    n=1e+1%number of matrices to be generated
    B = cell(1,n);%result cell
    i=1;
    while i<=n
        tmp=B;
        C=vec2mat(randsample(4,100,true),10)
        tmp{end+1}=C;
        if ~any(cell2mat(cellfun(@(x) isequal(x,A),tmp,'UniformOutput',false)))
            B{i}=C;
            i=i+1
        end
    end