【问题标题】:Vectorize some matlab code向量化一些matlab代码
【发布时间】:2015-10-09 16:28:34
【问题描述】:

我有以下 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

【问题讨论】:

    标签: matlab


    【解决方案1】:

    第一个 for 循环可以向量化如下。第二个可以类似地完成。

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

    【讨论】:

    • 另一个 for 循环呢?
    • 好吧,如果你理解我为第一个写的内容,那么使用./ 对第二个进行矢量化就很简单了。但是通过再次查看您的第一个循环,我不确定您要在这里做什么 - 您将某个值乘以 1?
    • 我尝试了你的建议,但它似乎不起作用:/你能给我一些其他的解决方案吗?我收到尺寸不一致的错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多