reducing computational time by removing global variable
1 view (last 30 days)
Show older comments
hello, i wanted to know whats the best way to get rid of the use of 'global" and still be able to run this code as it takes alot of time to get results because of the use of 'global'.
Lx = 101; % Defines the size of the lattice . Ly = 425; pt =rand(425,101); pfront=zeros(425,101); IM = ones(Ly,Lx).*2; % Initialization of the lattice % IM(i,j)=2 t =1; tend =1; % Number of "time steps". k = 1; % Counting index for front propagation . q = 1; % Counting index for image writing . % Initial step. The invader starts from a "point" or a channel %IM(Ly,50)=1; % Point IP IM(Ly,1:Lx)=1; % Channel IP % Finds the indices of the largest element in a given row in pt , % and invades (=1) the corresponding element in IM IM(Ly-1,pt(Ly-1,1:Lx)==max(pt(Ly-1,1:Lx))) = 1; while tend==1 if max(IM(2,1:end))==1 break end % Check if there are trapped clusters IM=bwlabel(IM-1,4); % Labels defending clusters , by setting invader sites = 0. IM=IM+1; % Keeps the labeling , and puts the invader sites = 1. for i = 1:Ly s = sort(IM(i,1:end)); % Sorts the i?th row in ascending order. if s(1)==1 % Tests if the i ? th row contains an invader site . for j = 1:Lx bin = front(i-1,j); % If the i?th row contains an invader site , all the elements in the (i?1)?th row are % being tested in the function front .m to % see if they are part of the front . if bin==1 % pfront is the front matrix where the first column % contains the probability and the second and third % columns contain the corresponding indices for this front %site. pfront(k,1) = pt(i-1,j); pfront(k,2) = i-1; pfront(k,3) = j; k = k+1; end end end end % Probability . % y?coordinate. %x?coordinate. % Finds the row?index of the largest probability in pfront , % and invades (=1) the corresponding element in IM e = find(pfront(:,1)==max(pfront(:,1))); IM(pfront(e,2),pfront(e,3)) = 1; clear pfront k = 1; % Put invader sites = 1, and defending sites = 0. IM(IM ~= 1) = 0; % The following lines saves the image for each iteration . if ((t/1000)==q) pic = label2rgb(IM(2:end,1:end)); number = int2str(q); picloc = strcat('f:\',number,'.tif'); imwrite ( pic , picloc ,'tif') ; q = q+1; end t = t+1; end
function [bin] = front(a,b) global IM global Lx global Ly % The site is part of the front if one of the neighboring sites % is an invader. if IM(a,b)~=IM(1,1) % Takes trapped clusters into account . If the site % tested is not part of the large defending cluster it % should not be counted as a front site . bin=0; elseif (b+1)==(Lx+1) if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b-1)==1) bin = 1; else bin = 0; end elseif (b-1)==(1-1) if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1) bin = 1; else bin = 0; end elseif (a+1)==(Ly+1) if (IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1; else bin = 0; end elseif (a-1)==(1-1) if (IM(a+1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1; else bin = 0; end else if (IM(a+1,b)==1)||(IM(a-1,b)==1)||(IM(a,b+1)==1)||(IM(a,b-1)==1) bin = 1; else bin = 0; end end end
5 Comments
Stephen23
on 15 Jun 2018
"and yes they are the cause of the slow processing"
How are you testing this?
Accepted Answer
Guillaume
on 15 Jun 2018
function [bin] = front(a, b, IM, Lx, Ly)
if IM(a,b)~=IM(1,1)
%... rest of code as it was
end
Then in your script instead of
bin = front(i-1,j);
use
bin = front(i-1, j, IM, Lx, Ly);
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!