I am getting more time complexity ,can you possibly minimize the code
1 view (last 30 days)
Show older comments
please review the attached code below,and give me some suggestions to reduce the time complexity
0 Comments
Answers (6)
Rik
on 22 Feb 2018
for k = 1 : numel(h)
indexH = floor(8 * h(k));
indexS = floor(2 * s(k));
indexV = floor(2 * v(k));
h(k)=indexH;
s(k)=indexS;
v(k)=indexV;
end
Replace it with:
h=floor(8*h);s=floor(2*s);v=floor(2*v);
Another part you can optimize easily is this:
for i=1:72
A(i)=length(find (G==i));
end
You should replace this with something like histcounts, you might even not have to reshape your array.
Otherwise, use the profiler to find out what part of your code is the bottleneck and optimize/rethink that. You should be aware that reading from disk is very slow compared to the rest of the operation Matlab does. Also, you last loop doesn't do anything useful, but imshow is very slow. image is much faster in general, because it puts a lot of management task on you (clearing the axes when appropriate etc.).
4 Comments
Walter Roberson
on 22 Feb 2018
Rik is the person who mentioned "last loop". Rik was referring to
for z=1:999
for M=1:12
if (CP(z)==B(M))
subplot(5,3,M+3)
img = fullfile(pathname, sprintf('%d%s', z, str) );
imshow( img )
title( sprintf('z = %d', z) )
end
end
drawnow();
end
I explained below why this is a waste of time, that the functionality can be replaced by sort() and a simple loop.
The issue about imshow() is that imshow() does a lot of work on your behalf, some of which is not necessary to do if you know what you are doing with image() and calls to axes(). As your concern at this time is performance rather that design, you should replace
imshow( img )
with
image_data = imread(img);
image(image_data);
axis image equal
Walter Roberson
on 22 Feb 2018
You sort CP into B and then you do a double nested loop looking for places the 12 smallest sorted values equal the unsorted values. That is a waste of time. Instead when you do the sorting, use the two output form of sort() as that returns indices directly and no searches would be needed.
2 Comments
Walter Roberson
on 1 Mar 2018
Replace
for z=1:999
for M=1:12
if (CP(z)==B(M))
subplot(5,3,M+3)
img = fullfile(pathname, sprintf('%d%s', z, str) );
imshow( img )
title( sprintf('z = %d', z) )
end
end
drawnow();
end
with
[~, sortidx] = sort(CP);
for M = 1 : 12
z = sortidx(M);
img = fullfile(pathname, sprintf('%d%s', z, str) )
subplot(5,3,M+3)
imshow( img )
title( sprintf('z = %d', z) )
end
teja jayavarapu
on 28 Feb 2018
Edited: teja jayavarapu
on 28 Feb 2018
8 Comments
Jan
on 2 Mar 2018
@teja: Meanwhile I'm completely confused by this thread. I find some code, some explanations and some inputs distributes over the question, some answers and some comments. I suggest to open a new thread, include all requires information in the question, provide the code directly as text instead of an attachment, but attach the input data. Then add a link to this thread here and note, that the new thread is a clarification and summary.
teja jayavarapu
on 5 Mar 2018
2 Comments
Jan
on 6 Mar 2018
@Pavan teja: Who are you? This is the thread of teja jayavarapu, so why do you ask for help here?
Please do not make it hard to read your code. As you can see in the answers to your question, a nicely formatted code is much better to read. See http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup. Currently your code is such ugly, that it hurts to read it.
Walter and Rik have posted some suggestions already and you did not post a comment. Do the ideas work?
It is still not clear to me, what you are asking for. Do you want to simplify the code to make it nicer to read or to decrease the runtime?
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!