# How to speedup assignment of indexed large matrix?

25 views (last 30 days)
Shikun Chen on 16 Jul 2019
Edited: Bruno Luong on 16 Jul 2019
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?

Walter Roberson on 16 Jul 2019
Are you truly assigning the same scalar to 100 different positions? Or does your actual code have you generating 100 different values to assign in?
Shikun Chen on 16 Jul 2019
Actually, the assigned values are calculated from complex expressions. They are usually not the same.

Bruno Luong on 16 Jul 2019
Edited: Bruno Luong on 16 Jul 2019
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

#### 1 Comment

Shikun Chen on 16 Jul 2019
That's great. Changing the order of dimension indeed reduces the time cost. Thank you for the advise.