Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Sped up for loop

5 ビュー (過去 30 日間)
Dat Tran
Dat Tran 2016 年 2 月 23 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Dear all,
Please show me how to speed up the for loops below. It took me a day to run for M=50.
I would appreciate your help! Best, Dat
clear all;
clc;
M = 20;
A_pro_cct = zeros(16,M);
tm = zeros(16,16);
s = zeros(17,4);
for N = 1:M
for ii = 1:N
filename = 'C:\Users\Dat\Documents\CT_TMs_counties.xlsx';
sheet = ii;
tm = xlsread(filename,sheet,'b2:q17');
s = xlsread(filename,sheet,'h21:k37');
for i = 1:16
pro_cct = s(i,1)*tm(i,9)+tm(i,3)*s(i,3) + tm(i,1)*s(i,1);
A_pro_cct (i,N) = pro_cct;
end
end
end
AA = A_pro_cct';

回答 (1 件)

Steven Lord
Steven Lord 2016 年 2 月 23 日
You're reading the Microsoft Excel spreadsheet file M*(M+1) times in your double nested FOR loop. Instead pull those lines of code outside the double FOR loops, read the file into a variable once (or once per sheet) and refer to the appropriate section of that variable inside the loop.
  5 件のコメント
Dat Tran
Dat Tran 2016 年 2 月 24 日
Thank a lot!!!
Image Analyst
Image Analyst 2016 年 2 月 24 日
As I understand it, if you have R2015b or later, the Excel reading will be a lot faster because it does not shutdown the Excel server after xlsread(), and so does not have to launch it again either.
And cell arrays take a huge amount of memory and a lot of overhead. You don't even need to use cell arrays at all. Just use regular numerical arrays.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by