Dividing each value in a matrix row by a value in the corresponding row of a vector

1 view (last 30 days)
Hi,
Please excuse me if this is difficult to understand - I am new to Matlab and coding in general.
I have a 3x1 vector and a 3x3 matrix. I would like to solve an equation using each value in each row of the matrix, but the equation also requires using the corresponding row value in the vector. For instance, I need to get three solutions using the three values in first row of the matrix while using the value (1,1) in the vector, then get three solutions in the second row using the value (2,1) in the vector and so on.
The equation (which may be coded wrong depending on the answer) is:
Sigma = sqrt(log(1+(Y/Z(i))^2));
For example, if I have vector Y = [1;2;3] and matrix Z = [2 4 6; 8 10 12; 14 16 18] I want to the solutions to:
Sigma = sqrt(log(1+(1)/2)^2)); Sigma = sqrt(log(1+(1)/4)^2)); Sigma = sqrt(log(1+(1)/6)^2));
and
Sigma = sqrt(log(1+(2)/8)^2)); Sigma = sqrt(log(1+(2)/10)^2)); Sigma = sqrt(log(1+(2)/12)^2));
and so on.
Ideally, I would like to do this in a single step, for example using a loop that would give me all the answers in a new matrix. However, I have not been successful after trying many different approaches. Other solutions also welcome, of course. Any advice is much apprciated.
Thanks, Charles
  1 Comment
Matt J
Matt J on 10 Mar 2013
Is i=sqrt(-1) in this context? Also, in
Sigma = sqrt(log(1+(1)/2(i))^2));
does 2(i) mean that both 2 and i are in the denominator?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 10 Mar 2013
Edited: Matt J on 10 Mar 2013
f=@(a,b) sqrt(log(1+a./b).^2);
Sigmas=bsxfun(f,Y,Z)
  2 Comments
Noah
Noah on 10 Mar 2013
Super helpful, thanks! The (i) was just an error (now removed), so I modifed the solution accordingly and it worked perfectly:
f=@(a,b) sqrt(log(1+(a./b).^2)); Sigmas=bsxfun(f,Y,Z)

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!