Jump to content
    

Векторизация MatLab

Есть следующий цикл в MatLab:

crc = zeros(1, 8);

crc_new = zeros(1, 8);

for i = 1:8
                
    indices = nonzeros(index_matrix(i, :))';

    xor_sum = 0;

    for idx = 1:length(indices)
        bit_ind = indices(idx);
        xor_sum = xor(xor_sum, crc(bit_ind));
    end

    for j = 1: length(D(i, :))
        xor_sum = xor(xor_sum, D(i, j));
    end

    crc_new(i) = xor_sum;

end

Нужно с помощью векторизации matlab по возможности упростить следующие циклы:

for idx = 1:length(indices)
     bit_ind = indices(idx);
     xor_sum = xor(xor_sum, crc(bit_ind));

end

 

for j = 1: length(D(i, :))
     xor_sum = xor(xor_sum, D(i, j));

end

В результате xor_sum содержит один итоговый бит; Длины векторов indices и (D(i, :)) не совпадают;

Пример вектора indices: [1,4,5,7]

Пример матрицы D : [0;0;1;1;0;0;0;1]

 

 

Share this post


Link to post
Share on other sites

11 hours ago, looser said:

crc_new(i)=mod(sum(crc(indices))+sum(D(i,:)),2)

Тогда, возможно, это можно переписать даже во что-то типа:

crc_new= arrayfun( @(i) mod(sum(crc(indices))+sum(D(i,:)),2) , 1:8 );

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...