ailoher
ailoher

Reputation: 67

Replacing elements in a base matrix by squared permutation submatrices

I have a sparse parity check matrix (consisting of ones and zeros) and I would need to replace each nonzero element (ones) from it by PM: a squared permutation matrix of dimension N (being N generally a large integer). In the case of the zero elements, these would be replaced by squared null matrices of the same dimension.

Let me share with you which the current state of my code is:

This is the base matrix in which I would like to replace its ones by permutation matrices:

B = zeros((L + ms) * dc, L * dv);

for i = 1 : 1 : L
    for j = 1 : 1 : dv       
        B(dc*(i-1)+1 : dc*(ms+i), j+dv*(i-1)) = ones(dc*(ms+1), 1);            
    end
end

I have been told a way for doing so by using 'cell' objects, this is, initializing H as an array of empty cells that would contain the corresponding submatrices:

H=repmat({{}},size(B));
Mc = 500 % Dimension of the permutation matrix
MP=randi([1,5],Mc,Mc); % Definition of one permutation matrix
% It would be desirable that the permutation matrix is different for each replacement
[H{B==0}]=deal(zeros(Mp));
[H{B==1}]=deal(MP);

But there's one problem coming up -that I would need this matrix to be used as a parameter of a following function and it would be very much desirable that it were a simple matrix of ones and zeros (as I am not very familiar with 'cell' structures... however, as you can see, Mc is such a big integer that I don't know if that would be possible to be handled.

Do you have any other way of doing so to have a raw matrix of dimensions (L*ms)dcMc, LdvMc as output?

These are some parameters that could be used to have a try:

ms = 2;
Mc = 600; % any number (specially big ones) could serve for this purpose
dc = 3;
dv = 4;
L = 15;

Many thanks in advance for your attention, and may you have a nice day.

Upvotes: 0

Views: 103

Answers (1)

Wolfie
Wolfie

Reputation: 30046

This is how it can be done with cells:

B = Randi([0,1], m, n);    % m*n array of 1s and 0s 
H = cell(size(B));         % Define cell array
Mc = 500;                  % Size of replacement matrices
MP = randi([1,5], Mc, Mc); % Permutation matrix

H(B==0) = {zeros(Mc, Mc)}; % Set elements of H where B==0 to matrix of zeros
H(B==1) = {MP};            % Set elements of H where B==1 to permutation matrix

% Convert to matrix
Hmat = cell2mat(H);        % Hmat = Mc*m row by Mc*n column matrix 

Each cell element holds a matrix of size Mc*Mc, at the end this can be converted to one large matrix. Take care which sorts of brackets you use with cells, the parentheses () are for logical indexing, whilst the curly braces {} are for assigning the sub-matrixes as cell elements.

Example:

B  = [1 0; 1 1];
MP = [1 2; 3 4];
H(B==0) = {zeros(2, 2)};
H(B==1) = {MP};     
Hmat = cell2mat(H);       

% >> Hmat =  [1  2  0  0
%             3  4  0  0
%             1  2  1  2
%             3  4  3  4];

If you want the replacement matrix MP to change, you will have to do this in a loop, changing MP on each iteration and using it to replace one element of H.

Upvotes: 1

Related Questions