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

 채택된 답변

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

태그

질문:

2022년 1월 7일

답변:

2022년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by