### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## meshgrid values to function without loop

Christopher Brokmann

### Christopher Brokmann (view profile)

さんによって質問されました 2019 年 1 月 30 日

### Rik (view profile)

さんによって 回答されました 2019 年 1 月 30 日
Rik

### Rik (view profile)

さんの 回答が採用されました
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

Nimananda Sharma

### Nimananda Sharma (view profile)

2019 年 1 月 30 日
Hi,
It appears that your function accepts only scalar input
One possible solution would to change your function to use maxtrix?
Christopher Brokmann

### Christopher Brokmann (view profile)

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 (view profile)

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.

サインイン to comment.

R2017b

## 1 件の回答

### Rik (view profile)

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 件のコメント

サインイン to comment.

Translated by