Filter löschen
Filter löschen

logical matrix divided by double matrix is slow

2 Ansichten (letzte 30 Tage)
Chaoyang Jiang
Chaoyang Jiang am 21 Mai 2018
Kommentiert: Image Analyst am 21 Mai 2018
a=logical(12,90,111);
b=randi(100,[12,90,111]);
c = a ./ (b+1e-10).
This line seems a very simple operation but it is so time-consuming. I am wondering if any speed up can be done?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Mai 2018
You are executing that line 8675040 times and it is taking 84367.53 seconds to do so, which is an average of 0.00972531884579207 seconds per iteration. Is that slow? 12*90*111 = 119880 divisions are done each time, for an average of 8.1125449164098e-08 seconds per division. Is that slow?
The code internally has to do
double(a) ./ (b+1e-10)
which is an extra operation compared to the case where a is already double. Those extra operations take time.
That said, my tests do tend to suggest that doing the double() yourself is faster, by up to a factor of 5. Try
c = double(a) ./ (b+1e-10);
  1 Kommentar
Chaoyang Jiang
Chaoyang Jiang am 21 Mai 2018
Oh, yes, you are right. the double operation speed up the code by 5 times. Thank you very much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 21 Mai 2018
What is the last line in your post???
When I run your code I get this:
Error using logical
Too many input arguments.
Error in test2 (line 1)
a=logical(12,90,111);
When I do this:
a=false(12,90,111);
b=randi(100,[12,90,111]);
tic;
c = a ./ (b+1e-10);
toc
I get
Elapsed time is 0.000511 seconds.
which seems pretty fast to me. What are you doing differently?
  2 Kommentare
Chaoyang Jiang
Chaoyang Jiang am 21 Mai 2018
1 For the error, the first line should be changed to a=zeros(12,90,111,'logical') to make the code run.
2 This line will run 8675040 times, which takes a longer time.
Image Analyst
Image Analyst am 21 Mai 2018
Well change it to false, which is also a logical, and it will run so much faster.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by