Not sure that I understand. If you wanted to create a rectangular array whose elements are a the result of some arithmetic operation between corresponding elements of A and B, you could use the fact that MATLAB expands automatically operands of such operations (from 2016b on, otherwise you have to use BSXFUN):
>> A = 0 : 2 ; B = -2 : 2 ;
>> A.' .* B
ans =
0 0 0 0 0
-2 -1 0 1 2
-4 -2 0 2 4
Note that here we first make A a 3x1 column vector by transposition. Then we apply an element-wise operation ( .* ) between this transpose and B that is 1x5, and MATLAB expands automatically A.' along B and vice versa (hence building 3x5 arrays) and performs the operation.
If you needed to apply a function that is not an arithmetic operation, you could do it using BSXFUN (performing an explicit call to a function that does an implicit expansion..):
>> result = bsxfun( @plus, A.', B )
result =
-2 -1 0 1 2
-1 0 1 2 3
0 1 2 3 4
or through an explicit expansion (using REPMAT, REPELEM, or MESHGRID):
>> [Bx, Ax] = meshgrid( B, A )
Bx =
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
Ax =
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
applying whatever function to the two arrays:
>> plus( Ax, Bx )
ans =
-2 -1 0 1 2
-1 0 1 2 3
0 1 2 3 4
댓글 수: 3
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/375904-how-can-i-create-a-matrix-with-the-values-of-the-elements-is-a-function-of-the-indices#comment_522065
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/375904-how-can-i-create-a-matrix-with-the-values-of-the-elements-is-a-function-of-the-indices#comment_522065
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/375904-how-can-i-create-a-matrix-with-the-values-of-the-elements-is-a-function-of-the-indices#comment_522067
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/375904-how-can-i-create-a-matrix-with-the-values-of-the-elements-is-a-function-of-the-indices#comment_522067
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/375904-how-can-i-create-a-matrix-with-the-values-of-the-elements-is-a-function-of-the-indices#comment_522080
이 댓글에 대한 바로 가기 링크
https://kr.mathworks.com/matlabcentral/answers/375904-how-can-i-create-a-matrix-with-the-values-of-the-elements-is-a-function-of-the-indices#comment_522080
댓글을 달려면 로그인하십시오.