Efficient implementation of a functions with vector arguments
1 次查看(过去 30 天)
显示 更早的评论
I am an inexperienced MATLAB user. I need to define functions with an efficient implementation because they will be called many times. The function arguments are 3-D vectors, and the functions need access to individual elements of the vectors. The following is a toy example.
function y = foo( vector )
y = vector(1)*cos(vector(2))/(1+vector(3));
end
--------------------------
Then I could do
v = [2,pi/3,2];
foo(v)
ans = 0.3333
Is there a more efficient way to implement the above function?
Thanks, Ted Ersek
1 个评论
Sean de Wolski
2013-4-3
What does your real vector look like and what would you expect for the output?
采纳的回答
Doug Hull
2013-4-3
编辑:Doug Hull
2013-4-3
Do not worry about efficiency until you know it is a problem. I think you might be surprised where the bottlenecks in your code are. First write clear, correct, maintainable code. When it is found to be slow, then you can optimize for speed. You will likely find that the maintainable code is faster than you need.
I would be VERY surprised if this code was the slowdown in any realistic code.
Doug
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!