performance - Slow array operations -
i'm quite new matlab programmer, might easy one.. :)
i'm trying generate script, able read number of xyz-files, in order, array, , arrange them in array according x , y coordinates given in file.. attempt use load files array, , after that, read through array and, explained, use x , y coordinate locations in new array..
i've tried presetting array size, , i'm subtracting value both x , y minimize size of array (fullarray)
%# script extraction of xyz-data dsm/dtm xyz files %# define folders , filter dsmfolder='/share/cfdwork/site/ofsites/mabh/dsm/*.xyz'; dtmfolder='/share/cfdwork/site/ofsites/mabh/dtm/*.xyz'; %# define minimumvalues, reduce arrays.. please leave slack, %# reduction-algorithm.. borderx=100000; bordery=210000; %% expected array-size expsizex=20000; expsizey=20000; %# program starts.. please not edit below line! files=ls(dsmfolder); clear fullarray fullarray=zeros(expsizex,expsizey); minx=999999999; miny=999999999; maxx=0; maxy=0; disp('reading dsm files'); [thisfile,remaining]=strtok(files); while (~isempty(thisfile)) disp(['reading: ' thisfile]); clear fromfile; fromfile=load(thisfile); k=1:size(fromfile,1) tic fullarray(fromfile(k,1)-borderx,fromfile(k,2)-bordery)=fromfile(k,3); disp([k size(fromfile,1)]); if (fromfile(k,1)<minx) minx=fromfile(k,1); end if (fromfile(k,2)<miny) miny=fromfile(k,2); end if (fromfile(k,1)>maxx) maxx=fromfile(k,1); end if (fromfile(k,2)>maxy) maxy=fromfile(k,2); end toc end [thisfile,remaining]=strtok(remaining); end as can seen, i've added tic-toc, , time 3.36secs 1 operation!
any suggestion on, why slow, , how improve speed.. need order 2x6,000,000 lines, , can't bothered wait 466 days.. :d
best regards mark
have considered using sparse matrix?
a sparse matrix in matlab defined list of values , location in matrix - incidentally matches input file perfectly.
while representation meant matrices sparse, (i.e. of values zeros), appears in case faster load matrix using sparse function if not sparse.
since data organised in such way (location of every data point) guess is sparse anyway.
the function create sparse matrix takes location columns instead of loop code (this segment replaces whole loop):
minx = min(fromfile(:,1); maxx = max(fromfile(:,1); miny = min(fromfile(:,2); miny = max(fromfile(:,2); s = sparse(fromfile(:,1) - borderx, fromfile(:,2) - bordery, fromfile(:,3)); note other change i've made calculating minimum / maximum values directly matrix - faster going on loop, operating on vectors , matrices unleashes true power of matlab :)
you can perform sorts of operations on sparse matrix, if want convert regular matrix can use matlab full function.
Comments
Post a Comment