function image = z_fast(images, camX, camY, camZ, disp, apature)
%My failed attempt to implement a matrix version of z
%Trying to speed up calculations, as matlab is very fast with matrices, but
%very slow with for loops. 
%However, I ran out of time, and this is not trivial, so this code DOES NOT
%WORK 


rows = size(images, 1);
cols = size(images, 2);

image = images{1,1};
image(:,:,:) = 0;

pix_rows = size(image,1);
pix_cols = size(image,2);

[U V] = meshgrid(1:pix_cols, 1:pix_rows); 

U_WORLD = ((2*(U - .5*pix_cols))/pix_cols)+camX;
V_WORLD = ((2*(V - .5*pix_rows))/pix_rows)+camY;
S = camX - camZ*(U_WORLD - camX);
T = camY - camZ*( camY - V_WORLD); 

S = max(1, min(S, cols));
T = max(1, min(T, rows));

[SCAMS TCAMS] = meshgrid(1:cols, 1:rows);
SCAMS = reshape(SCAMS, 1,  rows*cols);
TCAMS = reshape(TCAMS, 1, rows*cols);

%nearest neighbor until guassian works
S = round(S);
T = round(T);


images = cell2mat(images);

indices_col = ((S-1)*pix_cols)+U;
indices_row = ((T-1)*pix_rows)+V;

for i=1:pix_rows
    for j=1:pix_cols
        image(i,j,:) = image(i,j,:)+images(indices_row(i,j), indices_col(i,j), :);
    end
end


end