Here is the problem:
I need to repeatly assign a given value to part of a large matrix, assume the following test code 1:
%% Test code 1
clearprofile onA=zeros(1e8,10);for i=1:1000 idx=randi(1e8,100,1); a=rand(); A(idx,:)=a; % This line takes 0.912s.
endprofile viewer% total time is 0.924s,
From the profiling, it could be seen that the assigment takes the major time of computation. But as shown in test code 2:
%% Test code 2
clearprofile onA=zeros(100,10); % This line takes the major time.
for i=1:1000 a=rand(); A(:,:)=a; endprofile viewer% total time is 0.004s
Assignment of data to a same size reduced matrix only takes negligible time cost compared with the code 1. It indicate the indexing into the large matrix may be the bottleneck of the algorithm.
So is there any way to speedup the indexing process? Or is there any alternative way to make the similar assignment?
Best Answer