How to speedup assignment of indexed large matrix?

11 ビュー (過去 30 日間)
Shikun Chen
Shikun Chen 2019 年 7 月 16 日
編集済み: Bruno Luong 2019 年 7 月 16 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 16 日
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 2019 年 7 月 16 日
Actually, the assigned values are calculated from complex expressions. They are usually not the same.

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2019 年 7 月 16 日
編集済み: Bruno Luong 2019 年 7 月 16 日
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 件のコメント
Shikun Chen
Shikun Chen 2019 年 7 月 16 日
That's great. Changing the order of dimension indeed reduces the time cost. Thank you for the advise.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by