フィルターのクリア

Matlab Busy since four hours

1 回表示 (過去 30 日間)
Muhammad Ali Haider
Muhammad Ali Haider 2016 年 4 月 1 日
コメント済み: Steven Lord 2019 年 5 月 24 日
Here is my code which when run makes the matlab show busy since hours. What can be wrong? My data is big though!
Y = importdata('Dr_Kucukvar_data.xlsx');
linverse = importdata('L_inverse.txt');
X= Y.data.Diagonal;
mlinverse_1 = X(:,1);
mlinverse_1 = diag(mlinverse_1);
mlinverse_2 = X(:,2);
mlinverse_2 = diag(mlinverse_2);
mlinverse_3 = X(:,3);
mlinverse_3 = diag(mlinverse_3);
mlinverse_4 = X(:,4);
mlinverse_4 = diag(mlinverse_4);
mlinverse = [mlinverse_1 mlinverse_2 mlinverse_3 mlinverse_4];
Result_matrix = zeros(7824,1);
for i= 1:4
u= 7824*(i-1)+1;
g= u +7823;
for j= 0:26
for k= 96:107
h= 163*j + k;
z= zeros(7824,1);
z(h)= 1;
S = mlinverse(:,u:g) * linverse * z;
Result_matrix = [Result_matrix S];
end
end
end
  3 件のコメント
Geoff Hayes
Geoff Hayes 2016 年 4 月 1 日
Muhammad - look at the innermost loop iterating over k
h= 163*j + k;
z= zeros(7824,1);
z(h)= 1;
So on every iteration, you are re-creating z to be a 7824x1 array just to set one element, z(h), to one. Why? Is this intended?
Muhammad Ali Haider
Muhammad Ali Haider 2016 年 4 月 1 日
Thank you so much Geoff. Stafford's code below really worked!

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

採用された回答

Roger Stafford
Roger Stafford 2016 年 4 月 1 日
Your method of appending columns to 'Result_matrix' is very time consuming. Also as Geoff points out, your creation of a new 'z' matrix each time in the inner loop is very inefficient. I suggest you alter your code as follows:
......
Result_matrix = zeros(7824,1297); % 1297 = 4*27*12+1
c = 1;
for i= 1:4
u= 7824*(i-1)+1;
g= u +7823;
for j= 0:26
for k= 96:107
h= 163*j + k;
S = mlinverse(:,u:g) * linverse(:,h);
c = c+1;
Result_matrix(:,c) = S;
end
end
end
This should give you the same result and be considerably faster.
Final note: Do you really want the first column of Result_matrix to be all zeros?
  4 件のコメント
Stephen23
Stephen23 2019 年 5 月 24 日
Steven Lord
Steven Lord 2019 年 5 月 24 日
Preallocation will help, but this is also a problem that can be easily vectorized (unless this is a homework problem where you're required to use a for loop, in which case do preallocate as Stephen suggested.) See the Array Operations section on that page for an example that looks somewhat like the problem you're trying to solve.
I vectorized the code and the computation piece took under a minute on my machine, most of which was probably spent allocating the three 8 GB arrays needed to store n, en, and sol_err. I didn't let the plotting finish, since that's a lot of data to plot. It's probably more data points than you have pixels on your screen, so you might want to plot only every thousandth point or something. Or you could divide your upper limit Nex by a thousand or something. When I used Nex = 1000000 the whole operation was done in a couple seconds.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by