Vectorize nested for loop with matrix indexing
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Divye Kalra
am 8 Jan. 2022
Kommentiert: Divye Kalra
am 8 Jan. 2022
Hello everyone, the code shown below works as intended. It's just that it takes quite a bit of time to execute.
Note : T is a 512x3 array of doubles, Z1 is a 512x512 array of doubles and Z1_encrypt = Z1 OR T
Z1_encrypt = [];
Z2_encrypt = [];
Z3_encrypt = [];
for i = 1:1:512
disp(i)
for j = 1:1:512
Z1_encrypt = [Z1_encrypt bitor(Z1(j,i),T(j,1),'uint64')];
Z2_encrypt = [Z2_encrypt bitor(Z2(j,i),T(j,2),'uint64')];
Z3_encrypt = [Z3_encrypt bitor(Z3(j,i),T(j,3),'uint64')];
end
end
I was looking into vectorization of code to speed it up and went over quite a few examples. One very similar example can be found here, but it was of not much help to me.
It would be great if someone could help me out here.
0 Kommentare
Akzeptierte Antwort
Chunru
am 8 Jan. 2022
% The following code is slow, because the array size is growing. Memory
% allocation will slow down the program
% Z1_encrypt = [];
% Z2_encrypt = [];
% Z3_encrypt = [];
% for i = 1:1:512
% disp(i)
% for j = 1:1:512
% Z1_encrypt = [Z1_encrypt bitor(Z1(j,i),T(j,1),'uint64')];
% Z2_encrypt = [Z2_encrypt bitor(Z2(j,i),T(j,2),'uint64')];
% Z3_encrypt = [Z3_encrypt bitor(Z3(j,i),T(j,3),'uint64')];
% end
% end
% ry the following for speed
Z1_encrypt = bitor(Z1,T(:,1),'uint64');
Z2_encrypt = bitor(Z1,T(:,2),'uint64');
Z3_encrypt = bitor(Z1,T(:,3),'uint64');
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!