フィルターのクリア

Vectorize a code involving multiple loops commands

1 回表示 (過去 30 日間)
Askeladden2
Askeladden2 2020 年 8 月 10 日
コメント済み: Askeladden2 2020 年 8 月 10 日
Dear All Community members,
I have created a code involving three loops and a while command. I want to vectorize the code because it involves combining large matrices and is far too slow to run.
A short explanation;
I want to estimate a result using a mathematical operation combining three matrices that corresponds to a fixed value.
I.e.; I run a range of dummy values ("x") through a mathematical formula based on two large matrices ("a" and "b") and further multiply these results with another matrix "c".
I create a new matrix established by the dummy values and the corresponding sums above. (I can perhaps avoid this matrix using vectorization?)
The result is found by the dummy results when the corresponding sum is equal to the fixed value "d".
A simple example of the code is given below:
x = [0:0.1:10];
a=[2 1; 0.5 0.25];
b=[500 200; 300 250];
c=[0.25 0.35; 0.15 0.25];
d=[0.5];
for k = 1:length(x);
for i = 1:length(a)
for j = 1:length(a)
ab(i,j) = exp(-exp(-((x(k)-a(i,j)*sqrt(2*log(b(i,j))))/(a(i,j)/(sqrt(2*log(b(i,j))))))));
abc(i,j) = ab(i,j) * c(i,j);
end
end
%sum
abcSum = sum(sum(abc));
x(k);
TotSum(k) = abcSum;
end
i = 1;
%target d
while (1 - TotSum(i)) > (d)
i = i + 1;
end
%Result
Result = x(i);
The results shal be 3.2.
I appreciate all help and tips.
Thanks in advance!
  2 件のコメント
Stephen23
Stephen23 2020 年 8 月 10 日
編集済み: Stephen23 2020 年 8 月 10 日
"I want to vectorize the code because it involves combining large matrices and is far too slow to run."
Why do you think that vectorized code would be faster? Vectorizing code involving large array is liable to have the opposite effect: it could easily make your code slower due to the generation of large intermediate arrays.
In any case, the complete lack of array preallocation shows that your current loops are not optimised at all, you are likely to get much better returns for your time by improving your existing code (e.g. preallocate those arrays, run the profiler, replace the while loop with find, etc.).
Askeladden2
Askeladden2 2020 年 8 月 10 日
Thank you for the swift reply.
"Why do you think that vectorized code would be faster? Vectorizing code involving large array is liable to have the opposite effect: it could easily make your code slower due to the generation of large intermediate arrays."
I want to compare these methods since I can do fast gpu computations.
Thank you for the link and tip with replacing while loop with find. I am trying to optimise my code but I wanted to do a comparison with vectorization at the same time.

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

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 10 日
編集済み: Bruno Luong 2020 年 8 月 10 日
x = [0:0.1:10];
a=[2 1; 0.5 0.25];
b=[500 200; 300 250];
c=[0.25 0.35; 0.15 0.25];
d=[0.5];
X = reshape(x,1,1,[]);
AB = exp(-exp(-((X-a.*sqrt(2*log(b)))./(a./(sqrt(2*log(b)))))));
ABC = AB.*c;
TotSum = reshape(sum(ABC,[1 2]),1,[]);
i = find(1 - TotSum <= d, 1, 'first')
Result = x(i)
  1 件のコメント
Askeladden2
Askeladden2 2020 年 8 月 10 日
Thank you very much Bruno!
Your code is working perfectly!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by