I've been struggling for a while to find a good solution to the problem described below. I want to avoid for loops but feel my Matlab skills are inadequate to do otherwise. I have a 3D position matrix of size 329x230x105. This defines a 3d volume of 10000x7000x3100 meters. Most of the matix elements are zero except for a subvolume:
I need to construct a mask of the same size as my original matrix which is divided in large submatices, each defining a regular subvolume of 1000x1000x1000 meters and assign 1 to all the elements in a submatrix which contains at least one on-element (non-zero) from my original matrix. Viewed in XY :
So the end result is a 3D mask (figure below in XY) where all elements within marked cells (red) have value 1 and elements outside are set to 0 :
Please note that I am not interested in the volumes bounding box or the convex hull vertices.
Many thanks in advance.
Added information, answer to @grantnz:
well, I am not yet sure if the following code does work in all situations, but here is what I do (and it takes nearly 10 seconds on my laptop):
% get subscripts of non-zero 3d grid cells
[u v w]=ind2sub(size(tmpT),find(tmpT)); % tmpT is the original 230x329x105 matrix
increm = 30.480061; % grid cell size in meters
U=(u-1)*increm; % convert subscripts to position in meters
V=(v-1)*increm;
W=(w-1)*increm;
U=U/1000; % round down to nearest 1000 meter
V=V/1000;
W=W/1000;
U=floor(U);
V=floor(V);
W=floor(W);
U=U*1000;
V=V*1000;
W=W*1000;
U=round(U/increm); % find subscripts of 1000 meter cells
V=round(V/increm);
W=round(W/increm);
U(U==0)=1; % make sure no zero
V(V==0)=1;
W(W==0)=1;
IX=[U V W]; % make subscript matrix
[q,i,j]=unique(IX,'rows'); % find unique vectors in subscript matrix
myMask=zeros(size(tmpT)); % initiate mask matrix
oneKM = 33; % this many cells in 1000 meters
for i=1:length(q) % for each position vector, set mask (from:from+1000 meter) to 1
myMask(q(i,1):q(i,1)+oneKM,q(i,2):q(i,2)+oneKM,q(i,3):q(i,3)+oneKM)=1;
end