2015-10-09 33 views
0

我有以下matlab代碼,我想用no寫for循環: 代碼將滿足一定條件的assings值賦給稀疏矩陣。二,JJ是稀疏矩陣索引。向量化一些matlab代碼

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 

回答

0

第一個for循環可以被矢量化如下。第二個可以類似地完成。

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

另一個for循環呢? – yaron

+0

好吧,如果你明白我爲第一個寫的是什麼,那麼直接使用'。/'向量化第二個。但通過再次查看你的第一個循環,我不確定你在這裏試圖做什麼 - 你用1乘以一些東西? – Yugong

+0

我嘗試了您的建議,但似乎沒有奏效:/您能否給我一些其他解決方案。我得到錯誤,維度不同意。 – yaron