Re: can we vectorize this



vihang wrote:


Hello
This is from the user contributed file found here

<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=4927&objectType=file>.

A part of the code is shown below.

I need to know whether this could be vectorised, so that it could
run
faster and be more memory efficient.

% Create y-slice
if (ind==2 || is3DTexture)
x = [model.xdata(1), model.xdata(1); model.xdata(2),
model.xdata(2)];
y = [model.ydata(1), model.ydata(1); model.ydata(1),
model.ydata(1)];
z = [model.zdata(1), model.zdata(2); model.zdata(1),
model.zdata(2)];
diff = model.ydata(2)-model.ydata(1);
delta = diff/size(cdata,1);
for n = 1:size(cdata,1)

slice = double(squeeze(cdata(n,:,:)));
h(handle_ind) = surface(x,y,z,'Parent',ax);


set(h(handle_ind),'cdatamapping','scaled','facecolor','texture','cda
ta
',slice,...


'edgealpha',0,'alphadata',double(slice),'facealpha','texturemap','ta
g'
,'vol3d');
y = y + delta;
handle_ind = handle_ind + 1;
end
end
%%%%%%%%%
Thanks and regards
Vihang

Vectorizing code can often be SLOWER and LESS memory efficient than
FOR loops. Without digging too far, I can assume Joe (MathWorks
employee) is fully aware of this and knows when/how to vectorize.

I suggest emailing Joe through his author link and asking if he has
any suggestions on how to speed his code.
.