Efficient implementation of a functions with vector arguments

조회 수: 1 (최근 30일)
Ted Ersek
Ted Ersek 2013년 4월 3일
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
Sean de Wolski 2013년 4월 3일
What does your real vector look like and what would you expect for the output?

댓글을 달려면 로그인하십시오.

채택된 답변

Doug Hull
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개)

카테고리

Help CenterFile 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!

Translated by