Applying a function using elements of a vector and storing the results as elements in a matrix

Hi,
my aim is to use the elements of a vector for a function and store the results as elemets in a matrix.
I have a vector of size 10 (f.e.)
v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
My function is: u(i,j) = v(i)*v(j)
I would like to itereate through the elements of the vector and multiply them, once for the rows and once for columns of the matrix (so basicly, I would like to take the first element of the vector and multiply it with each element of the vector and store the results in the first row of the matrix, and then repeat this process for all elements of the vector.)
Usually, I program with Python and therefore, I would use for loops but I would like to find a faster solution as my original matrix is 1000x1000.
Thanks, for your help.
Greetings
Anna

 Accepted Answer

v = [1, 2, 3, 4, 5];
u=v.'*v
u = 5×5
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25

3 Comments

Thanks for the answer. I just realized that the function I chose as an example is a little bit to easy for what I aim to do.
The original function I have is : u(i,j) log(v(i)^0.5 + 0.9*v(i) - v(j)
I was not able to replicate your answer with his function. Do you maybe have an idea for this function?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!