Speed up code:: Code is working but slow..

2 ビュー (過去 30 日間)
Mark Sc
Mark Sc 2021 年 2 月 25 日
編集済み: per isakson 2021 年 2 月 26 日
Hi ,
The code is working fine, but it's very slow... any suggestions ?
clearvars;
clc;
C(1:3,1:3,1:3,1:3) = rand(3,3,3,3)
T = [cos(theta) sin(theta) 0;
-sin(theta) cos(theta) 0;
0 0 1];
T_z = T;
tensor_order = ndims(C);
el_n = numel(C);
C_tran = C;
C_tran(:) = 0;
iie = zeros(tensor_order,1); % initialise vector with indices of input tensor element
ioe = zeros(tensor_order,1); % initialise vector with indices of output tensor element
cum_vector = cumprod(3*ones(tensor_order,1))/3;
for i = 1:el_n, % loop over all output elements
ioe = mod(floor((i-1)./cum_vector),3)+1; % calculate indices of current output tensor element
for j = 1:el_n, % loop over all input elements
pmxx = 1; % initialise product of transformation matrices
iie = mod(floor((j-1)./cum_vector),3)+1; % calculate indices of current input tensor element
for k = 1:tensor_order % loop over all dimensions
pmxx = pmxx * T_z( ioe(k), iie(k) );
end
C_tran(i) = C_tran(i) + pmxx * C(j); % add product of transformation matrices and input tensor element to output tensor element
end
end

回答 (1 件)

per isakson
per isakson 2021 年 2 月 25 日
編集済み: per isakson 2021 年 2 月 25 日
  2 件のコメント
Mark Sc
Mark Sc 2021 年 2 月 25 日
Thanks for your answer... actually, I still do not know why, and which lines are the main problem.. I also asked for any modifications in the code, specially converting sum functions into a for loop
per isakson
per isakson 2021 年 2 月 26 日
編集済み: per isakson 2021 年 2 月 26 日
"but it's very slow" Do you mean more than one millisecond? What is an acceptable execution time?
"specially converting sum functions into a for loop" The function, sum(), doesn't appear in your code.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by