yaron
yaron

Reputation: 117

How to iterate over elements in a sparse matrix in matlab?

I am writing a code for an LDPC belief propogation decoder in matlab. I am following the algorithm specified in: http://www.ece.umd.edu/~tta/resources/LDPC.pdf page 6.

I want to use sparse matrix toward this goal. During which i want to iterate over large sized matrix which has only few ones. My question is how can i iterate over only the ones in the matrix?

I'll be more specific: let's say i have the following matlab code:

for row_index = 1 : row_num
    for coloum_index = 1 : col_num
        if parity_check_matrix(row_index, coloum_index)
            messages_llr_matrix(row_index, coloum_index) = ...
                code_word_aprior_prob(coloum_index);
        end
    end

This code takes a vector 'code_word_aprior_prob' and assigns it's values to the row in the matrix 'messages_llr_matrix' which are not zeros. Now, i want to perform this using sparse matrix.

parity_check_matrix is a sparse matrix.

Upvotes: 2

Views: 3261

Answers (2)

Wauzl
Wauzl

Reputation: 971

The following does what your code snippet does, but doesn't exactly iterate over a sparse matrix, like the title of the question suggests. Also, I'm not sure if the approach in my other answer is faster or slower than this.

Note that I assume, that code_word_aprior_prob is a row vector. Then doing

messages_llr_matrix = bsxfun(@times, parity_check_matrix, code_word_aprior_prob)

will probably be enough.

Please test out if this actually gives the correct answer and please test out which way is faster, if you are really aiming for speed.

Upvotes: 0

Wauzl
Wauzl

Reputation: 971

Suppose you have a sparse matrix S. You can iterate over its nonzero elements using

[ii,jj,ss] = find(S);

for k=length(ii)
   %// A nonzero element of S: ss(k) = S(ii(k),jj(k))
end

Although I'm not sure how fast this would be.

In your specific case you would probably be doing

[ii,jj] = find(parity_check_matrix);
for k = 1 : length(ii)
   messages_llr_matrix(ii(k), jj(k)) = ...
            code_word_aprior_prob(jj(k));
end

Of course you can rename ii and jj to be row_index and column_index resp.

Upvotes: 8

Related Questions