フィルターのクリア

Why is this nested for loop faster in python? How can I make it faster in Matlab?

2 ビュー (過去 30 日間)
Jose Zamora Zeledon
Jose Zamora Zeledon 2016 年 6 月 6 日
編集済み: Stephen23 2016 年 6 月 6 日
...code above...
J = zeros(n_x,n_x,n_x); %Preallocation
ionf = zeros(n_x,n_x,n_x); %Preallocation
dens = zeros(n_x,n_x,n_x); %Preallocation
temp = zeros(n_x,n_x,n_x); %Preallocation
%loop
for i = 1:n_x
for j = 1:n_x
for k = 1:n_x
data = strsplit(fgets(fin));
J(i,j,k) = single(str2double(data{1}));
ionf(i,j,k) = single(str2double(data{2}));
dens(i,j,k) = single(str2double(data{3}));
temp(i,j,k) = single(str2double(data{4}));
end
end
end
...code below...
I translated some code from python to Matlab. Why is this nested for loop faster in python? How can I make it faster in Matlab? All variables are defined, I just need advice in the for loop speed. Thank you in advance
  1 件のコメント
Stephen23
Stephen23 2016 年 6 月 6 日
編集済み: Stephen23 2016 年 6 月 6 日
WHY: because they are different languages and work in different ways. It is slow because it uses a nested loops and slow conversions inside those loops (e.g. str2double).
FIX: Just replace the all of this code with textscan and it will be much faster.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by