Slow while loop generating coordinates in 3d matrix
Mostrar comentarios más antiguos
Hi Matlab community,
I'm pulling cross-validation data from a 3-d matrix (a satellite image time series). The way I'm doing this (matlab novice here) is generating x,y and z random integer coordinates and testing to make sure I have a value at this point, in addition to testing for duplicates. However, using 'unique' to test for row duplicates inside my while loop really slows it down - i.e. it takes 1 hour to generate ~8600 points from a 650x450x120 matrix (though to be fair, about 85% this matrix is missing data).
What this loop looks like:
[m,n,o]=size(before);
%before is the 650x450x120 matrix
coordinate=[];
k=1;
%crossnum is generally between 18,000 and 51,000, ~4% of available points
while k<=crossnum
%x
coordinate(k,1)=randi(m,1);
%y
coordinate(k,2)=randi(n,1);
%timeslice
coordinate(k,3)=randi(o,1);
%Make sure unique values and no NaNs
coordinatetest=unique(coordinate,'rows');
[i,j]=size(coordinatetest);clear coordinatetest j;
if (~isnan(before(coordinate(k,1),coordinate(k,2),coordinate(k,3))) && (i==k))
k=k+1;
else
k=k;
end
%disp(k)
end
All the modifications I've tried so far (i.e. preallocating 'coordinate', using a boolean test combined with 'sum' instead of unique) made it much slower. If anyone has suggestions on a more efficient way of doing this I would be really appreciative. Thanks in advance!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!