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

4 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2013-3-10
编辑:Matt J 2013-3-10
f=@(a,b) sqrt(log(1+a./b).^2);
Sigmas=bsxfun(f,Y,Z)
  2 个评论
Noah
Noah 2013-3-10
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)

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by