MATLAB 中的元胞数组操作 --- 创建关系矩阵
cell arrays manipulations in MATLAB --- creating a relation matrix
我有两个元胞数组,分别命名为 countryname
和 export
。
countryname
中只有一栏,是国家名称的代码:
USA
CHN
ABW
export
中有两列:
USA ABW
USA CHN
CHN USA
ABW USA
一行export
中的每一对(X,Y)表示“X国与Y国有关系”。 countryname
的大小已经简化为3,请问如何在MATLAB中实现如下?
Create a square 3 by 3 (in general n by n, where n is the size of countryname
) matrix M such that
M(i,j)=1
if country i has relation with country j
M(i,j)=0
otherwise.
国家/地区名称在 countryname
中重新标记为正整数。
您需要做的第一件事是建立从国家名称到从 1 到 3 的整数值的映射。您可以使用 containers.Map
来实现,其中输入是字符串,输出是一个整数。因此,我们将 'USA'
分配给 1,'CHN'
分配给 2,'ABW'
分配给 3。假设您已经像上面提到的那样初始化元胞数组:
countryname = {'USA', 'CHN', 'ABW'};
export = {'USA', 'ABW'; 'USA', 'CHN'; 'CHN', 'USA'; 'ABW', 'USA'};
...您可以像这样创建一个 containers.Map
:
map = containers.Map(countryname, 1:numel(countryname));
一旦你有了这个,你只需将国家名称映射到整数,你就可以使用 values
function to help you do this. However, what will be returned is a cell array of individual elements. We need to unpack the cell array, so you can use cell2mat
。因此,我们现在可以创建一个 4 x 2
索引矩阵,其中每个单元格元素都转换为数值:
ind = cell2mat(values(map, export));
我们因此得到:
>> ind
ind =
1 3
1 2
2 1
3 1
现在我们有了这个,您可以使用 sparse
to create the final matrix for you where the first column serves as the row locations and the second column serves as the column locations. These locations will tell you where it will be non-zero in your final matrix. However, this will be a sparse matrix and so you'll need to convert the matrix to full
最终得到一个数值矩阵。
M = full(sparse(ind(:,1), ind(:,2), 1));
我们得到:
>> M
M =
0 1 1
1 0 0
1 0 0
作为更方便的表示,您可以创建一个 table
to display the final matrix. Convert the matrix M
to a table using array2table
我们可以将行和列名称添加为国家名称本身:
>> T = array2table(M, 'RowNames', countryname, 'VariableNames', countryname)
T =
USA CHN ABW
___ ___ ___
USA 0 1 1
CHN 1 0 0
ABW 1 0 0
请注意,上面创建 table
的代码仅适用于 MATLAB R2013b 及更高版本。如果那不是您所需要的,只需坚持使用原始数字矩阵 M
.
这仅使用基本的 MATLAB 功能。 @rayryeng 上面发布的解决方案肯定更先进,并且编码速度也可能更快。但是,这也应该有助于您从根本上理解
clear
country={'USA','CHN','ABW'};
export={'USA' 'ABW'; 'USA' 'CHN'; 'CHN' 'USA' ; 'ABW' 'USA'};
M=zeros(length(country));
for i=1:length(country)
c=country(i);
ind_state=strfind(export(:,1),char(c)); % this gives state of every which is 1 or blank.
ind_match=find(not(cellfun('isempty', ind_state))); % extracting only indices which are 1.
exp_match=export(ind_match,2); % find corresponding export rel countries from second column
% useful only when your first ind_match matrix has more than 1 element.
% Like 'USA' appears twice in first column of export countries.
for j=1:length(exp_match)
c=exp_match(j);
ind_state=strfind(country,char(c));
ind_match=find(not(cellfun('isempty', ind_state)));
M(i,ind_match)=1; % Selective make elements of M 1 when there is match.
end
end
M
我有两个元胞数组,分别命名为 countryname
和 export
。
countryname
中只有一栏,是国家名称的代码:
USA
CHN
ABW
export
中有两列:
USA ABW
USA CHN
CHN USA
ABW USA
一行export
中的每一对(X,Y)表示“X国与Y国有关系”。 countryname
的大小已经简化为3,请问如何在MATLAB中实现如下?
Create a square 3 by 3 (in general n by n, where n is the size of
countryname
) matrix M such that
M(i,j)=1
if country i has relation with country j
M(i,j)=0
otherwise.
国家/地区名称在 countryname
中重新标记为正整数。
您需要做的第一件事是建立从国家名称到从 1 到 3 的整数值的映射。您可以使用 containers.Map
来实现,其中输入是字符串,输出是一个整数。因此,我们将 'USA'
分配给 1,'CHN'
分配给 2,'ABW'
分配给 3。假设您已经像上面提到的那样初始化元胞数组:
countryname = {'USA', 'CHN', 'ABW'};
export = {'USA', 'ABW'; 'USA', 'CHN'; 'CHN', 'USA'; 'ABW', 'USA'};
...您可以像这样创建一个 containers.Map
:
map = containers.Map(countryname, 1:numel(countryname));
一旦你有了这个,你只需将国家名称映射到整数,你就可以使用 values
function to help you do this. However, what will be returned is a cell array of individual elements. We need to unpack the cell array, so you can use cell2mat
。因此,我们现在可以创建一个 4 x 2
索引矩阵,其中每个单元格元素都转换为数值:
ind = cell2mat(values(map, export));
我们因此得到:
>> ind
ind =
1 3
1 2
2 1
3 1
现在我们有了这个,您可以使用 sparse
to create the final matrix for you where the first column serves as the row locations and the second column serves as the column locations. These locations will tell you where it will be non-zero in your final matrix. However, this will be a sparse matrix and so you'll need to convert the matrix to full
最终得到一个数值矩阵。
M = full(sparse(ind(:,1), ind(:,2), 1));
我们得到:
>> M
M =
0 1 1
1 0 0
1 0 0
作为更方便的表示,您可以创建一个 table
to display the final matrix. Convert the matrix M
to a table using array2table
我们可以将行和列名称添加为国家名称本身:
>> T = array2table(M, 'RowNames', countryname, 'VariableNames', countryname)
T =
USA CHN ABW
___ ___ ___
USA 0 1 1
CHN 1 0 0
ABW 1 0 0
请注意,上面创建 table
的代码仅适用于 MATLAB R2013b 及更高版本。如果那不是您所需要的,只需坚持使用原始数字矩阵 M
.
这仅使用基本的 MATLAB 功能。 @rayryeng 上面发布的解决方案肯定更先进,并且编码速度也可能更快。但是,这也应该有助于您从根本上理解
clear
country={'USA','CHN','ABW'};
export={'USA' 'ABW'; 'USA' 'CHN'; 'CHN' 'USA' ; 'ABW' 'USA'};
M=zeros(length(country));
for i=1:length(country)
c=country(i);
ind_state=strfind(export(:,1),char(c)); % this gives state of every which is 1 or blank.
ind_match=find(not(cellfun('isempty', ind_state))); % extracting only indices which are 1.
exp_match=export(ind_match,2); % find corresponding export rel countries from second column
% useful only when your first ind_match matrix has more than 1 element.
% Like 'USA' appears twice in first column of export countries.
for j=1:length(exp_match)
c=exp_match(j);
ind_state=strfind(country,char(c));
ind_match=find(not(cellfun('isempty', ind_state)));
M(i,ind_match)=1; % Selective make elements of M 1 when there is match.
end
end
M