MATLAB: How to speedup assignment of indexed large matrix

indexinglarge matrixspeedup

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
clear
profile on
A=zeros(1e8,10);
for i=1:1000
idx=randi(1e8,100,1);
a=rand();
A(idx,:)=a; % This line takes 0.912s.
end
profile 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
clear
profile on
A=zeros(100,10); % This line takes the major time.
for i=1:1000
a=rand();
A(:,:)=a;
end
profile 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

In general on a computer there is a huge different between accessing contiguous memory block (fast, the later code) and non contiguous one (slower, accessing random rows).
Try to assign randomly on the column matrix of big number you might get another intermediate time.
A=zeros(1e7,10);
tic
for i=1:1000
idx=randi(size(A,1),100,1);
a=rand();
A(idx,:)=a;
end
toc % Elapsed time is 0.214560 seconds.
A=zeros(10,1e7);
tic
for i=1:1000
idx=randi(size(A,2),100,1);
a=rand();
A(:,idx)=a;
end
toc % Elapsed time is 0.077767 seconds
Related Question