How to change a formula so that it does element by element multiplication?

I am writing a script, and in that I have worked out a formula to be
rho = -1/(cos(theta) + sin(theta) - 2)
however... I need to it took like this
rho = -1./(cos(theta) + sin(theta) - 2)
(notice the dot in the second rho)
I need to do this without physically going in and changing it to a dot.

1 Comment

I think you're going to need to more explicit about what you mean by things like "I am writing a script" but that you cannot do something "physically".
For example, would a command-line tool like awk be something that could be used to solve your problem? Or is that "physically" doing something?

Sign in to comment.

Answers (1)

The purpose of the dot is to enable matlab to distinguish between different kinds of operations between elements of arrays, for example the difference between matrix multiplication and element-by-element multiplication between matrices. If you want to use matlab and don’t want to use the dot, you will have to reduce the operation to that between scalars, and for arrays that means using for loops for element-by-element operations.
In your case if theta is a vector do this:
rho = zeros(size(theta));
for k = 1:length(theta)
rho(k) = -1/(cos(theta(k)) + sin(theta(k)) - 2);
end

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 24 Mar 2017

Answered:

on 24 Mar 2017

Community Treasure Hunt

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

Start Hunting!