フィルターのクリア

Define a name of the variable using the value of other variable

1 回表示 (過去 30 日間)
alpedhuez
alpedhuez 2020 年 12 月 5 日
編集済み: Steven Lord 2020 年 12 月 5 日
Suppise I have data
Month Day Visitors
----------------------------
January 1 100
January 7 50
February 1 400
February 3 200
I want to create a variable that is a proxy for months such as
Month Day Visitors proxy_January proxy_February
------------------------------------------------
January 1 100 1 0
January 7 50 1 0
February 1 400 0 1
February 3 200 0 1
One way to do this is to define variables one by one for each month.
But I would like to know how I can let Matlab do this automatically.
I start with
set_of_month=unique(T.Month)
that will give me the set of months. Then I can iterate over the set of months.
But how can I define a variable using set_of_months? I like to do something like
for i=1:length(set_of_states)
for j=1:height(T)
if T.Month(j)=set_of_month(i)
T.set_of_month(i)=1
else
T.set_of_month(i)=0
end
end
end
Please advise.
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 5 日
Are the months categorical or cell array of character vectors?
alpedhuez
alpedhuez 2020 年 12 月 5 日
cell array

サインインしてコメントする。

採用された回答

Marco Riani
Marco Riani 2020 年 12 月 5 日
Please let me know if I understood correctly your question.
Month={'January'; 'January'; 'February'; 'February'};
set_of_month=unique(Month);
% Add a matrix of zeros to host the dummy variables
dum=zeros(length(Month),length(set_of_month));
T=[table(Month) array2table(dum)];
% Add the labels
T.Properties.VariableNames(2:end)=set_of_month;
% Add the ones in the corresponding position
for i=1:length(set_of_month)
for j=1:height(T)
if strcmp(T.Month{j},set_of_month{i})
T{j,i+1}=1;
end
end
end
disp(T)
% Month February January
% ____________ ________ _______
%
% {'January' } 0 1
% {'January' } 0 1
% {'February'} 1 0
% {'February'} 1 0
  3 件のコメント
alpedhuez
alpedhuez 2020 年 12 月 5 日
"You cannot assign numeric values to a datetime array."
alpedhuez
alpedhuez 2020 年 12 月 5 日
Got it. Thank you very much.

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2020 年 12 月 5 日
T.(set_of_month{i}) = value
  4 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 5 日
The implication is that set_of_month is not a cell array of character vectors as you had claimed.
Perhaps your month entries in the table are individual cells rather than character vectors. If so then
monthname = set_of_month{i}{1};
T.(monthname) = value
and monthname is something that you can strcmp() or ismember()
alpedhuez
alpedhuez 2020 年 12 月 5 日
Let me work on it.

サインインしてコメントする。


Steven Lord
Steven Lord 2020 年 12 月 5 日
編集済み: Steven Lord 2020 年 12 月 5 日
M = repelem(["January"; "February"], 2, 1);
D = [1; 7; 1; 3];
V = [100; 50; 400; 200];
T = table(M, D, V, 'VariableNames', ["Month", "Day", "Visitors"])
T = 4x3 table
Month Day Visitors __________ ___ ________ "January" 1 100 "January" 7 50 "February" 1 400 "February" 3 200
UM = unique(T.Month);
for whichMonth = 1:numel(UM)
T.("proxy_" + UM(whichMonth)) = T.Month == UM(whichMonth);
end
T
T = 4x5 table
Month Day Visitors proxy_February proxy_January __________ ___ ________ ______________ _____________ "January" 1 100 false true "January" 7 50 false true "February" 1 400 true false "February" 3 200 true false
Alternately the unstack function may do what you want (or something similar to what you want.)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by