Create index vector from grouping variable based on a condition

Hey!
I have a table with x variables that very in sign from - to +. I want to create a third column t.count in a table that includes -1 index for every negative x value, 0 for 0 and +1 for the positive x-values. The only similar command that I know is grip2idx but it doesn't do what I need in this case. Is there a way to do it ithout a for-loop?
x = [0 0.5 0.6 1 -0.03 -0.6 -0.8 0 0]'
y = [1:1:length(x)]'
t = table
t.x = x
t.y = y

 Réponse acceptée

x = [0 0.5 0.6 1 -0.03 -0.6 -0.8 0 0]' ;
y = zeros(size(x)) ;
y(x<0) = -1 ;
y(x>0) = +1 ;
T = table(x,y)
T = 9×2 table
x y _____ __ 0 0 0.5 1 0.6 1 1 1 -0.03 -1 -0.6 -1 -0.8 -1 0 0 0 0

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by