I have a 3D matrix (8x5x100) containing numerical data. I need to pass a 2D matrix (8x5) taken from the 3D matrix into another function and repeat this process 100 times(length of 3D matrix). My goal is to speed up this process as much as possible. Sorry, I cannot post my actual code.

Current code:

```
3dmatrix=ones(8,5,100);
for i=1:100
    output(i)=subfunction(3dmatrix(:,:,i));
end

```

I read about using arrayfun which may be faster than looping. Is this the correct implementation?

```
3dmatrix=ones(8,5,100); 
for i=1:100
    output(i)=arrayfun(@(x) subfunction(x),3dmatrix(:,:,i));
end

```

When I try to execute the code using this new method, I keep getting errors in my "subfunction" code. In "subfunction", it uses the size of the 2D matrix for its calculations. However, when I use the arrayfun method, it keeps reading the size as a 1x1 instead of 8x5, causing the rest of my code to crash, complaining about not being able to access certain parts of vectors since they are not calculated due to the size discrepancy. Am I passing in the matrix correctly?

What is the correct way of going about this with speed being imperative? Thanks!