如果您需要您在此处所说的数据表示:
2000 0 0.4 0.2 0.4
2001 0.3 0 0.2 0.5
您所做的如下,假设您有一个数据矩阵表示,其中包含您显示的行和列表示并命名为oldRepresentation
(这意味着行是观察值,列是公司、年份位置和市场份额)。每个公司都会有一个新的表示形式,其中每个独特的年份都有一行,按新月顺序排列,一列表示位置。它可能是也可能不是稀疏表示,仅在矩阵稀疏时才使用稀疏表示,而不是您在此处显示的情况。
这是一个更改表示的脚本,但请注意 stackoverflow 不能以这种方式工作。您必须自己编写代码,并将您的代码问题和遇到的问题告诉我们。既然您说您是初学者,那么这里有一个想法供您使用和学习 matlab 语法。用它来锻炼其他点,作为你需要的欧几里得距离。记住,你应该尽量让你的问题尽可能笼统。
oldRepresentation = [...
...% Firm Year Location Market_share
1 2000 1 0.1;...
1 2000 2 0.2;...
1 2000 3 0.5;...
1 2000 4 0.2;...
1 2001 1 0.3;...
1 2001 2 0.0;...
1 2001 3 0.2;...
1 2001 4 0.5;...
2 2000 1 0.0;...
2 2000 2 0.4;...
2 2000 3 0.2;...
2 2000 4 0.4;...
2 2001 1 0.1;...
2 2001 2 0.5;...
2 2001 3 0.3;...
1 2001 4 0.1];
% Firm information (if you know these information a priori, you can
% set them directly):
firmsNumbers = unique(oldRepresentation(:,1))'; % Get unique firm
% numbers (suppose you have a firma that doesnt have a representation,
% in this case you will jump it.
nFirms = numel(firmsNumbers); % Total number of firms
% Year information:
years = unique(oldRepresentation(:,2))'; % Get unique years
nYears = numel(years); % Total number of years:
% Location information:
location = unique(oldRepresentation(:,3))'; % Unique locations
nLocations = max(oldRepresentation(:,3)); % Total number of locations
newRepresentation = cell(1,nFirms); % Pre allocate holder for the new
% representation, one cell for each firma, sparse representation.
nonSparse = cell(1,nFirms); % Pre allocate holder for the new
% representation, one cell for each firma, non sparse representation.
for curFirm=firmsNumbers % Loop on the firms
firmLines=(oldRepresentation(:,1)==curFirm); % get lines which the
% firm appears
yearsOfOperation=oldRepresentation(firmLines,2); % get current firm
% operation years
% Transform the years to line indexes:
[~,lineIdx] = ismember(yearsOfOperation,years);
firmLocations=oldRepresentation(firmLines,3); % get current firm
% operation locations
newRepresentation{curFirm} = sparse(nYears,nLocations); % One line
% for each year, one column for each location (sparse matrix
% allocation).
nonSparse{curFirm} = zeros(nYears,nLocations);
marketShares=oldRepresentation(firmLines,4);
% Now we fill this firm market share:
for k=1:numel(yearsOfOperation)
newRepresentation{curFirm}(lineIdx(k),firmLocations(k)) = ...
marketShares(k);
nonSparse{curFirm}(lineIdx(k),firmLocations(k)) = ...
marketShares(k);
end
end
结果非稀疏表示:
>> nonSparse{1} % First firma, first line is the 2000 year, each column a location from 1 to 4.
ans =
0.1000 0.2000 0.5000 0.2000
0.3000 0 0.2000 0.1000
>> nonSparse{2} % Second firma
ans =
0 0.4000 0.2000 0.4000
0.1000 0.5000 0.3000 0
稀疏表示:
>> newRepresentation{1} % First firm
ans =
(1,1) 0.1000
(2,1) 0.3000
(1,2) 0.2000
(1,3) 0.5000
(2,3) 0.2000
(1,4) 0.2000
(2,4) 0.1000
>> newRepresentation{2} % Second firm
ans =
(2,1) 0.1000
(1,2) 0.4000
(2,2) 0.5000
(1,3) 0.2000
(2,3) 0.3000
(1,4) 0.4000