MATLAB Answers

How can I create a matrix with the values of the elements is a function of the indices?

조회 수: 84(최근 30일)
I am trying to create a matrix from two vectors (i.e. A = [0:2] and B = [-2:2]) where the values for each cell of the matrix is a function of the indices. I cannot see the path to get it there, any simple examples I can follow?
Thanks

  댓글 수: 3

Stephen Cobeldick
Stephen Cobeldick 6 Jan 2018
The indices of the elements of vectors A and B are:
Ai = 1:numel(A)
Bi = 1:numel(B)
Is that what you want?
Image Analyst
Image Analyst 6 Jan 2018
What is the function? Like the sum of the squares of A and B or something? Are you aware that your A and B don't have the same number of elements so you can't do a 1-to-1 correspondence between elements?
Benjamin Schuessler
Benjamin Schuessler 6 Jan 2018
Hi All,
Apologies for being convoluted. Let me try to explain more.
With the vectors I gave I know it won't give an nxn matrix.
This is the matrix I get:
C =
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
However, I would like to see something like
C =
0,-2 0,-1 0,0 0,1 0,2
1,-2 1,-1 1,0 1,1 1,2
2,-2 2,-1 2,0 2,1 2,2
Where the values inside each element will then become inputs to a function which output the function's result in the same size matrix shown above.
I hope this can shed more light!
Thanks!
*EDIT I should say that I used C = ndgrid(A,B) to create that matrix.

댓글을 달려면 로그인하십시오.

채택된 답변

Cedric Wannaz
Cedric Wannaz 6 Jan 2018
편집: Cedric Wannaz 6 Jan 2018
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

  댓글 수: 2

Benjamin Schuessler
Benjamin Schuessler 6 Jan 2018
MY MAN! After reading through your answer, this is exactly what I need. Unfortunately I don't have the convenience of using R2016a however BSXFUN should work just fine!
Much appreciated!
Cedric Wannaz
Cedric Wannaz 6 Jan 2018
Great, well the first input of BSXFUN is a function handle so you have a lot of flexibility here; you can even adapt the interface using an anonymous function.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by