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.

10 件のコメント

Stephen23
Stephen23 2019 年 9 月 3 日
"It does not seem to work the best this way. "
You could use a loop, but it is often simpler and more efficient to write vectorized code:
Rik
Rik 2019 年 9 月 3 日
And if you vectorize it, you can use implicit expansion, or use ndgrid to generate all combinations.
Cside
Cside 2019 年 9 月 4 日
編集済み: Cside 2019 年 9 月 4 日
this is the current vectorized code I tried to create:
[x,y] = ndgrid (2:4,2:4)
%%vectorized example
xy = rowsncolumns (:,1) ==x & rowsncolumns(:,2) == y;
result = mean (dataset_pfc_tar_57_n1(xy))
But the error was that matrix dimensions must agree, and an error in line 3. How should i change the ndgrid to fit the vectorized code?
FYI
rowsncolumns = 621 x 2 double
Walter Roberson
Walter Roberson 2019 年 9 月 4 日
You are attempting to compare a 621 x 1 vector to a 3 x 3 array output by ndgrid
Cside
Cside 2019 年 9 月 4 日
my inputs of x & y are meant to be scalar though, i.e. x = 2, y = 2. What could i write to allow the vectorized code to run x = 2:4 & y = 2:4?
darova
darova 2019 年 9 月 4 日
What is length of dataset_pfc_tar_57_n1?
Cside
Cside 2019 年 9 月 4 日
1 x 621 double
darova
darova 2019 年 9 月 4 日
Each value of (x,y) you want to compare to rowsncolumn?
>> combvec([2:4],[2:4])
ans =
2 3 4 2 3 4 2 3 4
2 2 2 3 3 3 4 4 4
If i understood you correctly output matrix should be of size 621 x 9 ?
Cside
Cside 2019 年 9 月 4 日
yes, for each value, i would compare it to rowsncolumn. the output at the end should only give me 9 x 1, or 1x 9, basically 9 values as the last line of code is the mean.
Walter Roberson
Walter Roberson 2019 年 9 月 4 日
After you use ndgrid to build x y you can
x = permute(x, [3 1 2])
and the same for y. Then assuming R2016b or later, the & would give a 3d result, 621 by 3 by 3
Now take that and .* by dataset and sum along the first dimension. Then sum the result of the & itself along the first dimension to get counts. ./ the two qualities. You should now have a 1 x 3 x 3 array of means.

サインインしてコメントする。

 採用された回答

darova
darova 2019 年 9 月 4 日

0 投票

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

3 件のコメント

Walter Roberson
Walter Roberson 2019 年 9 月 4 日
Ah, but how do you proceed from here to vectorize the mean() that has to be done for each x y pair?
darova
darova 2019 年 9 月 4 日
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
Cside
Cside 2019 年 9 月 5 日
thank you so much, I really appreaciate everyone's help :D

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2019 年 9 月 3 日

コメント済み:

2019 年 9 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by