I have a column in my table containing numerical data, where I want to make them into 4 specific groups in a new column in the same table. I do not know how to write the code, but have attempted the following failed codes. Please correct me.
num_rows = size(Table1.Time,1) %count the number of rows in Table 1
for i=1:1:num_rows
if T1.Time < 5
T1.TimeNew = 5; % Group 1, return in a new column in Table 1
elseif T1.Time >= 5 & T1.Time <15
T1.TimeNew = 15; % Group 2
elseif T1.Time >= 15 & T1.Time <25
T1.TimeNew = 25; % Group 3
else T1.TimeNew = 30; % Group 4 (for any data >=25)
end
end

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 7월 18일
편집: Andrei Bobrov 2017년 7월 18일

0 개 추천

ii = discretize(T.Time,[-inf,5,15,25,inf])
V = [5,15,25,30]';
T.TimeNew = V(ii);

댓글 수: 5

wesleynotwise
wesleynotwise 2017년 7월 18일
편집: wesleynotwise 2017년 7월 18일
Oh yes. This is what I am looking for. THANK YOU!!!
May I know what is wrong with the'for and if statements'? Or they are not supposed to be used in this way?
Andrei Bobrov
Andrei Bobrov 2017년 7월 18일
편집: Andrei Bobrov 2017년 7월 18일
with block if..elseif..else..end
for ii=1:size(T1.Time,1)%count the number of rows in T1
if T1.Time(ii) < 5
T1.TimeNew(ii) = 5; % Group 1, return in a new column in Table 1
elseif T1.Time(ii) >= 5 && T1.Time(ii) <15
T1.TimeNew(ii) = 15; % Group 2
elseif T1.Time(ii) >= 15 && T1.Time(ii) <25
T1.TimeNew(ii) = 25; % Group 3
else
T1.TimeNew(ii) = 30; % Group 4 (for any data >=25)
end
end
other "exotic" variant:
t = [T1.Time < 5,T1.Time >= 5 & T1.Time <15,T1.Time >= 15 & T1.Time <25];
T1.TimeNew = [t,~any(t,2)]*[5;15;25;30];
wesleynotwise
wesleynotwise 2017년 7월 18일
Alright, I saw my mistakes! Thanks once again :)
Jessica Blasco
Jessica Blasco 2018년 5월 14일
thanks!! He also helped me with a similar problem!! :)
Padmamalini  T H
Padmamalini T H 2020년 1월 31일
thank you sir. it helped my problem too

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

lyda
lyda 2019년 5월 19일

0 개 추천

I have one table with many raws and columns.
I want to create a new table.
if a cell=111 return to me this raw
I don't know how to write this.
code=0;
for i=2:1143; %column 2 to raw 1143
if everything(i)==111; %"everything" is a table with all features
.......
end
end
thank you!

카테고리

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

질문:

2017년 7월 18일

댓글:

2020년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by