MATLAB: Very slow for loop

for loopperformance

Hi, I am new to Matlab, i have more experience with C/C++
I am trying run this simple loop:
x=zeros([10001,10001,2]);
for t=1:2
for z =1:10000
for i =1:10000
x(z,i,t)=i+z+t+2;
end
end
end
x(5,5,1)
But is taking a while..imagine many of then inside a single algorithm. in C++ this loop runs in a few seconds…
Is there any other way to run this loop in Matlab faster???
Sorry if my question is silly, first time running Matlab. I tried some vectorization but for nested loops things get really complicated, especially with different loop sizes.
Thanks

Best Answer

You might be able to get a slight performance boost by changing how the memory is acessed ...
Basically x(z,i,t) might not be the same as x(t,z,i) or x(i,z,t).