필터 지우기
필터 지우기

meshgrid values to function without loop

조회 수: 5 (최근 30일)
Christopher Brokmann
Christopher Brokmann 2019년 1월 30일
답변: Rik 2019년 1월 30일
Hello, i want to give x,y coordinates to a self-written function. The current state is, that i use 2 loops. I think that this is a very ugly and computation consuming solution. Can someone help me to improve this?
x = linspace(0,1,11) ;
y = linspace(0,1,11) ;
[X,Y] = meshgrid(x,y) ;
for kk = 1:11
for nn = 1:11
Coordinate = [X(kk,nn) Y(kk,nn)];
Z(kk,nn) = f_SomeFunction(Coordinate,someConstant,...);
end
end
  댓글 수: 4
Christopher Brokmann
Christopher Brokmann 2019년 1월 30일
its a very long function (~2k lines) which only accepts scalar inputs... before i take the long challenge to change the function to matrix input, i want to change the loops.
Matt J
Matt J 2019년 1월 30일
편집: Matt J 2019년 1월 30일
But the loops don't have any computation in them except your call to f_SomeFunction. There's nothing else there to optimize.

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

채택된 답변

Rik
Rik 2019년 1월 30일
If you are not prepared to change the function to accept non-scalar inputs, you can use this code to implicitly use for-loops. This can be faster in some circumstances, and it may look cleaner (even if that is subjective).
Coordinate = [X Y];
someConstant_array=repmat(someConstant,size(Coordinate));
Z = arrayfun(@f_SomeFunction,Coordinate,someConstant_array);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by