absolute value of a vector

how can i determine the absolute value of a vector?
so for example
if i had x=[ -0.5 .3 .5 -0.5]
how would i determine the absolute value of each individual term in the vector?
i've tried using "abs" but it spits back one value.

 Respuesta aceptada

Roger Stafford
Roger Stafford el 12 de Feb. de 2014

1 voto

That isn't true of matlab's 'abs' function. For every value in x it will return its absolute value. For every individual complex number it will return the square root of the sum of the squares of its real and imaginary parts.
I suspect you have a false 'abs' function somewhere on your system that is being called instead of the true 'abs'. You had better check on that.

5 comentarios

Matt J
Matt J el 12 de Feb. de 2014
Use
>>which abs(x)
to find the version of abs() that is being called on x.
Kyle
Kyle el 12 de Feb. de 2014
here is my code. i'm trying to do abs(((yexact-y)/yexact)) i'm getting 1 value and i don't know why.
h=0.1; t(1)=0; y(1)=1; t=0:h:5;
for i=1:(numel(t)-1);
k=2-exp(-4*t(i))-2*y(i) ;
y(i+1)=y(i)+h*k;
end
plot(t,y)
xlabel('t')
ylabel('y')
title('Euler Method: Problem 1 ')
hold on
h2=0.001;
t2(1)=0;
y2(1)=1;
t2=0:h2:5;
for i=1:(numel(t2))-1;
k2=2-exp(-4*t2(i))-2*y2(i) ;
y2(i+1)=y2(i)+h2*k2;
end
plot(t2,y2,'g')
t3=[0:0.1:5];
yexact=1+0.5*exp(-4*t3)-0.5*exp(-2*t3);
plot(t3,yexact,'r')
Matt J
Matt J el 12 de Feb. de 2014
Note the missing '.'
abs(((yexact-y)./yexact))
Kyle
Kyle el 12 de Feb. de 2014
thank you!
Roger Stafford
Roger Stafford el 12 de Feb. de 2014
I'll expand Matt's answer. You have used matrix division in yexact-y)/yexact and for this case the result would be a single 1 x 1 value, not a vector.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Feb. de 2014

Comentada:

el 12 de Feb. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by