Substitue values with text

조회 수: 1 (최근 30일)
Leonor Vieira Dias
Leonor Vieira Dias 2021년 1월 29일
댓글: Leonor Vieira Dias 2021년 1월 29일
Hello,
I have a table of data. I wanted to create a column that would have written "early" or "late" according to the difference between two values.
So far I have been able to differentiate the values to get 1 or 0 whether the condition was met or not. I wanted to have text instead.
Can we subsitute 1 by "late" and 0 by "early"? Is there an easier way to do this, e.g. using if loop?
Many thanks
T.Final = (T.Early<T.Delay)

채택된 답변

Iuliu Ardelean
Iuliu Ardelean 2021년 1월 29일
편집: Iuliu Ardelean 2021년 1월 29일
Maybe this works for you:
early = [10 20 30];
delay = [11 19 31];
final = strings(1, 3); % create an empty string array with 1 line and 3 columns
final(early<delay) = "late";
final(early>=delay) = "early";
  댓글 수: 3
Leonor Vieira Dias
Leonor Vieira Dias 2021년 1월 29일
I got it, I forgot to put a T in front of it. Thank you very much!
Iuliu Ardelean
Iuliu Ardelean 2021년 1월 29일
awesome

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

추가 답변 (1개)

dpb
dpb 2021년 1월 29일
% create sample data table
T=table(randi([30,70],10,1)+rand(10,1),'VariableNames',{'Early'});
T.Final=T.Early+(randi([-20,20],10,1)+rand(10,1));
% the desired conditional categorical variable
STR=categorical(["Early";"Late"]);
% the engine
T.Final=STR((T.Early<T.Delay)+1);
A given realization of the table data here resulted in:
>> T.Final=STR((T.Early<T.Delay)+1)
T =
10×3 table
Early Delay Final
______ ______ _____
40.327 26.39 Early
68.584 58.236 Early
69.197 75.902 Late
37.909 25.636 Early
36.492 46.388 Late
33.04 35.818 Late
58.207 51.744 Early
69.131 74.646 Late
58.487 69.468 Late
69.074 79.39 Late
>>

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by