How to speedup assignment of indexed large matrix?

15 vues (au cours des 30 derniers jours)
Shikun Chen
Shikun Chen le 16 Juil 2019
Modifié(e) : Bruno Luong le 16 Juil 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?
  2 commentaires
Walter Roberson
Walter Roberson le 16 Juil 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
Shikun Chen le 16 Juil 2019
Actually, the assigned values are calculated from complex expressions. They are usually not the same.

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 16 Juil 2019
Modifié(e) : Bruno Luong le 16 Juil 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 commentaire
Shikun Chen
Shikun Chen le 16 Juil 2019
That's great. Changing the order of dimension indeed reduces the time cost. Thank you for the advise.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by