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!

 採用された回答

dpb
dpb 2017 年 3 月 17 日
編集済み: dpb 2017 年 3 月 17 日

0 投票

idx=find(isfinite(before)); % all valid locations in 3D array with data
[i,j,k]=ind2sub(size(before),idx(randperm(numel(idx),crossnum))); % sample _p_ unique from _N_
coordinate=[i j k]; % pack into output array
Eliminate the need for testing by
  1. Only use valid data locations to start, and
  2. Select desired number of unique locations from total number valid
  3. Retrieve index in original array associated with selected subset
Then, having chosen the desired number from a linear ordering of the valid locations, rearrange those linear indices back into the desired 3D coordinates. Done. :)
See
doc randperm % for details on how the selection works...

1 件のコメント

schckngs
schckngs 2017 年 3 月 17 日
THANK YOU!! 0.002 seconds... Beautiful!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2017 年 3 月 17 日

コメント済み:

2017 年 3 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by