矢量化一些matlab代码

时间:2015-10-09 16:28:34

标签: matlab

我有以下matlab代码,我想写没有for循环: 该代码将满足特定条件的值赋予稀疏矩阵。 II,JJ是稀疏矩阵指数。

    all_tanh_bits = ones(1, NUMBER_OF_EQ);
    %Calculating each equation multipicative factor.
    for index = 1 : length(II)
        all_tanh_bits(II(index)) = (all_tanh_bits(II(index)) * ...
            tanh(messages_matrix(II(index), JJ(index))/2));
    end

    for index = 1 : length(II)
        %not_relevant_mul is the elemnt to divide - so the
        %current node only uses other message nodes for
        %calculating the llr.
        not_relevant_mul = tanh(messages_matrix(II(index),  ...
                                                JJ(index)) / 2);

        check_matrix_mul = all_tanh_bits(II(index)) / not_relevant_mul;
        check_matrix(II(index), JJ(index)) = ...
            log((1 + check_matrix_mul) / (1 - check_matrix_mul));
    end

1 个答案:

答案 0 :(得分:0)

第一个for循环可以按如下方式进行矢量化。第二个可以类似地完成。

 all_tanh_bits(II) = all_tanh_bits(II) .* tanh(messages_matrix(sub2ind(size(message_matrix), II, JJ))/2);