function that takes a vector and returns negative numbers and position

henberry

I'm looking to create a function that will take a vector and return all the negative values in one vector and their position in another vector. So, for example,

% output:
v = [-1 4 6 2 -3]

% output:
vneg = [-1 -3]
pos  = [1 5].

any help is appreciated!

Wasi Ahmad

One simple solution to get the negative values and their index.

x = [4 3 -2 9 -7 31];

index = find(x<0);
-> index = 3 5

x_new = x(index);
-> x_new = -2 -7

Just change the condition in the find function as per your requirement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R: ifelse function returns vector position instead of value (string)

From Dev

Why function is generating negative numbers?

From Dev

Haskell multi function that takes two numbers and returns the product without using multiplication (*) or division (/) signs

From Dev

How to write a function that takes a positive integer N and returns a list of the first N natural numbers

From Dev

Javascript: function which takes in object, and returns list of all numbers described in object

From Dev

Having trouble creating a function that takes one argument, and returns a result of adding odd numbers between 1 and argument?

From Dev

function that takes in and returns multiple values

From Dev

function that takes iterables and returns a string

From Dev

Raising vector with negative numbers to a fractional exponent in R

From Dev

Why Rem operator in Elixir returns negative numbers?

From Dev

A function that returns an array, but that also takes a function as parameter

From Dev

Negative numbers returned in a factorial Function (Python)

From Dev

Matlab - "Buttord" function returns negative order

From Dev

Java Date getTime function returns negative value

From Dev

Is there a shorthand for a function that takes no parameters and returns nothing? AKA () -> ()

From Dev

A function that takes string parameter and returns integer pointer

From Dev

function that takes variable length as argument and returns tuple

From Dev

Find the position of first negative number in a R vector that may be entirely positive

From Dev

Function that receives iterators and returns vector

From Dev

Pass a vector of baseclass to a function which takes vector of superclass

From Dev

Python: negative numbers, when multiplying a matrix by a vector with numpy

From Dev

.toFixed() called on negative exponential numbers returns a number, not a string

From Dev

How to write a recursive method in java that takes in a positive or negative integer and returns the number of digits it has

From Dev

PostgreSQL position function returns all records

From Dev

What should the type signature be for a function that takes a function and returns another function that returns a handle to a thread?

From Dev

I need help on understading: Write a method, sum which takes an array of numbers and returns the sum of the numbers

From Dev

Is there a function to count all positive numbers in a vector?

From Dev

clojure assignment of variables in a loop function that returns a vector

From Dev

MATLAB: Function returns single value instead of vector

Related Related

  1. 1

    R: ifelse function returns vector position instead of value (string)

  2. 2

    Why function is generating negative numbers?

  3. 3

    Haskell multi function that takes two numbers and returns the product without using multiplication (*) or division (/) signs

  4. 4

    How to write a function that takes a positive integer N and returns a list of the first N natural numbers

  5. 5

    Javascript: function which takes in object, and returns list of all numbers described in object

  6. 6

    Having trouble creating a function that takes one argument, and returns a result of adding odd numbers between 1 and argument?

  7. 7

    function that takes in and returns multiple values

  8. 8

    function that takes iterables and returns a string

  9. 9

    Raising vector with negative numbers to a fractional exponent in R

  10. 10

    Why Rem operator in Elixir returns negative numbers?

  11. 11

    A function that returns an array, but that also takes a function as parameter

  12. 12

    Negative numbers returned in a factorial Function (Python)

  13. 13

    Matlab - "Buttord" function returns negative order

  14. 14

    Java Date getTime function returns negative value

  15. 15

    Is there a shorthand for a function that takes no parameters and returns nothing? AKA () -> ()

  16. 16

    A function that takes string parameter and returns integer pointer

  17. 17

    function that takes variable length as argument and returns tuple

  18. 18

    Find the position of first negative number in a R vector that may be entirely positive

  19. 19

    Function that receives iterators and returns vector

  20. 20

    Pass a vector of baseclass to a function which takes vector of superclass

  21. 21

    Python: negative numbers, when multiplying a matrix by a vector with numpy

  22. 22

    .toFixed() called on negative exponential numbers returns a number, not a string

  23. 23

    How to write a recursive method in java that takes in a positive or negative integer and returns the number of digits it has

  24. 24

    PostgreSQL position function returns all records

  25. 25

    What should the type signature be for a function that takes a function and returns another function that returns a handle to a thread?

  26. 26

    I need help on understading: Write a method, sum which takes an array of numbers and returns the sum of the numbers

  27. 27

    Is there a function to count all positive numbers in a vector?

  28. 28

    clojure assignment of variables in a loop function that returns a vector

  29. 29

    MATLAB: Function returns single value instead of vector

HotTag

Archive