MATLAB: Collapse 3d data set to 2d data set using indexer for 3rd dimension without loops

3d datacollapseindexer

I have a 3d data set. I want to collapse the 3rd dimension of this data set. I have a 2d data set which represents the points i want to keep in the collapsing of the 3d data set. How can I accomplish this without having to use loops? This is how I am doing it now (with loops):
data = rand(100,100,10);
[xx,yy] = meshgrid(1:100,1:100);
zz = round(.03*xx+.05*yy);
zz(zz<1) = 1;
for i = 1:size(data,1)
for j = 1:size(data,2)
collapsedData(i,j) = data(i,j,zz(i,j));
end
end

Best Answer

[yy, xx] = ndgrid(1:size(data,1), 1:size(data,2));
collapsedData = data(sub2ind(size(data), yy, xx, zz))