Search code examples
matlabmatrixgeneratoroctaveparity

Get dynamic rows of matrix based on the ones of another matrix (Matlab)


I am new to Matlab and I need some help. I want compute Parity Check Matrix and then to encode a codeword using Generator Matrix

My matrix is the following :

1 0 0 0 1 1 1
0 1 0 0 1 1 0
0 0 1 0 1 0 1
0 0 0 1 0 1 1

The codeword is 1 0 1 1.

My code in Matlab is as follow :

printf('Generator Matrix\n');
G = [
1 0 0 0 1 1 1;
0 1 0 0 1 1 0;
0 0 1 0 1 0 1;
0 0 0 1 0 1 1
]

[k,n] = size(G)

P = G(1:k,k+1:n)

PT = P'

printf('Parity Check Matrix\n');
H = cat(2,PT,eye( n-k ))


printf('Encode the following word : \n');
D = [1 0 1 1]

C = xor( G(1,:), G(3,:) , G(4,:) )

My problem is that I want to get dynamically the rows of G Matrix in order to make the xor operation. Could you help me please ?

Thanks a lot


Solution

  • You only need matrix multiplication modulo 2:

    C = mod(D*G, 2);
    

    Alternatively, compute the sum of the rows of G indicated by D, modulo 2:

    C = mod(sum(G(D==1,:), 1), 2);