vector not produced missing some '.' ?

something is wrong with this code
t=0:0.1:1;
g = (0.30/(2*(pi*t^3)^0.5))*exp((-0.30^2)/(4*t));
As the vector g never gets made, I'm sure I need to put some '.' somewhere but I really don't understand this whole '.' stuff

1 commentaire

Paulo Silva
Paulo Silva le 7 Avr 2011
Sean answer is great for such cases, also when there's one dot missing matlab tells you what's the operation in the error message:
mtimes % before *
mdivide % before /
mpower % before ^

Connectez-vous pour commenter.

 Réponse acceptée

Paulo Silva
Paulo Silva le 7 Avr 2011

0 votes

t=0:0.1:1;
g = (0.30./(2*(pi*t.^3).^0.5)).*exp((-0.30^2)./(4*t));

Plus de réponses (3)

Sean de Wolski
Sean de Wolski le 7 Avr 2011

1 vote

vectorize('g = (0.30/(2*(pi*t^3)^0.5))*exp((-0.30^2)/(4*t))');
A brainless way for us lazies.

4 commentaires

Paulo Silva
Paulo Silva le 7 Avr 2011
That's a good one, I must remember it :) +1 vote
but it too lazie, even for me lol
Matt Fig
Matt Fig le 7 Avr 2011
Even better(?) and definitely lazier.
eval(vectorize('g = (0.30/(2*(pi*t^3)^0.5))*exp((-0.30^2)/(4*t))'))
Paulo Silva
Paulo Silva le 7 Avr 2011
... ..- .--. . .-. .-.. .- --.. .. .
Sean de Wolski
Sean de Wolski le 7 Avr 2011
Eval, is the king of MATLAB laziness!

Connectez-vous pour commenter.

Matt Fig
Matt Fig le 7 Avr 2011

0 votes

As a general rule when dealing with operations on arrays, use a dot before every one of the three operators:
* / ^
If you do this you will be fine.
David Young
David Young le 7 Avr 2011

0 votes

If you don't understand the operators, the most straighforward thing is to use
g = (0.30 ./ (2 .* (pi .* t .^ 3) .^ 0.5)) .* exp((-0.30 .^ 2) ./ (4 .* t));
so that all the operators behave as elementwise operators (which I guess is what you want). If you understand the difference, you can omit some of the dots, but they do no harm.
It's not all that hard really: *, / and ^ carry out matrix operations (like matrix multiplication, as defined here), while .*, ./ and .^ operate point by point on the elements of the matrices.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by