Add a matrix of strings

조회 수: 1 (최근 30일)
alpedhuez
alpedhuez 2020년 12월 7일
댓글: Walter Roberson 2020년 12월 7일
% Add a matrix of zeros to host the dummy variables
dum=zeros(length(Month),length(set_of_month));
T=[table(Month) array2table(dum)];
will add a matrix of zeros. But I want to add a matrix of some strings like
--------------
United States
United States
Then how can one modify the above code?
**********
This is what I did:
dum=zeros(height(T),1)
dum1=num2cell(dum)
dum1(dum==0)={'US'}
T1=cell2table(dum1)
T=[T T1]

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 7일
T = table(Month);
T.Country(:) = "United States";
or consider
T = table(Month);
T.Country(:) = categorical("United States");
  댓글 수: 2
alpedhuez
alpedhuez 2020년 12월 7일
"Conversion to cell is not possible"
Walter Roberson
Walter Roberson 2020년 12월 7일
Month = randi(12, 7, 1);
T = table(Month);
T.Country(:) = "United States";
T
T = 7x2 table
Month Country _____ _______________ 1 "United States" 2 "United States" 5 "United States" 3 "United States" 3 "United States" 4 "United States" 4 "United States"
T2 = table(Month);
T2.Country(:) = categorical("United States");
T2
T2 = 7x2 table
Month Country _____ _____________ 1 United States 2 United States 5 United States 3 United States 3 United States 4 United States 4 United States
No problem -- not unless you already happened to have a table variable named Country; or unless you neglected to remove the table variables that resulted from your earlier experiments.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by