how to sort the vectors to positive n negative of same elements?

Hi All, say i have a vector v = [1,-2,3,4,5,6,7,8,9,77,44,88,99,-1,95,-6,-7,-8,-9] now i want to check element by element if there is a same element with opposite sign(1, -1) in the array and plot it as zero in their respective points in the x axis. Please note: i do not need to store the values in the variable and just need to plot it. How can i implement that?
thanks

 Respuesta aceptada

Stephen23
Stephen23 el 7 de Mzo. de 2017
Editada: Stephen23 el 7 de Mzo. de 2017
>> v = [1,-2,3,4,5,6,7,8,9,77,44,88,99,-1,95,-6,-7,-8,-9]
v =
1 -2 3 4 5 6 7 8 9 77 44 88 99 -1 95 -6 -7 -8 -9
>> out = v .* ~ismember(v,-v)
out =
0 -2 3 4 5 0 0 0 0 77 44 88 99 0 95 0 0 0 0
>> plot(out)

3 comentarios

Hi Stephen, thanks for your help, but i need to plot only those numbers which are same(both positive and negative) as a positive point in x axis. like the points which got zero i need them to be plotted as a positive number on the x axis. Just the opposite of what the output you gave thanks
Stephen23
Stephen23 el 7 de Mzo. de 2017
Editada: Stephen23 el 7 de Mzo. de 2017
Then do the opposite:
out = v .* ismember(v,-v)
If neither of these resolve your question then please explain exactly what you want together with example input and output data. Your original question states "..if there is a same element with opposite sign(1, -1) in the array and plot it as zero..", and that is exactly what my answer gives you: values which are both positive and negative in the vector are plotted as zero.
thanks Stephen

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 7 de Mzo. de 2017

Comentada:

el 7 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by