For command in function
이전 댓글 표시
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
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
2019년 9월 3일
And if you vectorize it, you can use implicit expansion, or use ndgrid to generate all combinations.
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
2019년 9월 4일
darova
2019년 9월 4일
What is length of dataset_pfc_tar_57_n1?
Cside
2019년 9월 4일
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
2019년 9월 4일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!