1 view (last 30 days)

I have a function attached and would like it to automatically run it with x = 2:4 & y = 2:4, that is, the function to run 8 times & have 8 outputs, with inputs

x =2, y = 2

x = 2, y = 3

x = 2, y = 4

x = 3, y = 2

...

Should I write a 'for' command with the function? It does not seem to work the best this way.

darova
on 4 Sep 2019

You can use pdist2()

[X,Y] = meshgrid(2:4);

x = X(:);

y = Y(:);

D = pdist2(rowsncolumns,[x y]);

% D(:,1) - first column of D - distance from (rowscolumns) to (x1,y1) point

Indices you want: D == 0

Size of D: 621 x 9

Walter Roberson
on 4 Sep 2019

Ah, but how do you proceed from here to vectorize the mean() that has to be done for each x y pair?

darova
on 4 Sep 2019

Maybe not to use mean()

ix = D == 0;

data = repmat(dataset_pfc_tar_57_n1,[1 9]);

result = sum(data.*ix) ./ sum(ix);

size of result is 1 x 9

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 10 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741636

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741636

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741643

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741643

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741977

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741977

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741995

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741995

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741997

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_741997

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742024

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742024

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742034

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742034

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742040

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742040

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742151

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742151

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742165

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/478654-for-command-in-function#comment_742165

Sign in to comment.