arrayfun 2d matrix input

berrycrunch

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!

sleeping_dragon

Did you look at the arrayfun documentation? I don't think you need to use the loop when using arrayfun. Also have you considered using parfor loops? You could use them to make your code faster too..

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related