【发布时间】:2020-04-21 10:17:17
【问题描述】:
假设我有这个当前矩阵:
mat=[
{'Blue'} {'Heavy'} {'Rounded'};
{'White'} {'Light'} {'Square'};
{'Green'} {'Light'} {'Rounded'};
{'Blue'} {'Very Heavy'} {'Square'};
{'White'} {'Light'} {'Rounded'};
{'Green'} {'Very Heavy'} {'Square'};
{'Blue'} {'Light'} {'Rounded'};
{'White'} {'Very Heavy'} {'Square'};
{'Green'} {'Very Heavy'} {'Rounded'};
{'Blue'} {'Heavy'} {'Square'}];
首先我需要像这样创建矩阵的输出:
Color Weight Form
_______ ____________ _______
V1 'Blue' 'Heavy' 'Rounded'
V2 'White' 'Light' 'Square'
V3 'Green' 'Light' 'Rounded'
V4 'Blue' 'Very Heavy' 'Square'
V5 'White' 'Light' 'Rounded'
V6 'Green' 'Very Heavy' 'Square'
V7 'Blue' 'Light' 'Rounded'
V8 'White' 'Very Heavy' 'Square'
V9 'Green' 'Very Heavy' 'Rounded'
V10 'Blue' 'Heavy' 'Square'
我想要做的是将这个矩阵中的元素转换为二进制值
即类似的东西:
codageVar =
0 1 0 0 1 0 0 1
1 0 0 1 0 0 1 0
0 0 1 0 0 1 0 1
0 1 0 0 1 0 1 0
1 0 0 1 0 0 0 1
0 0 1 0 0 1 1 0
0 1 0 0 1 0 0 1
1 0 0 1 0 0 1 0
0 0 1 0 0 1 0 1
0 1 0 0 1 0 1 0
例如蓝色是{010}、白色{100} 和绿色{001} 如您所见,在这种情况下没有秩序感 我们不能说蓝色>白色或蓝色
但是对于列权重 => Heavy{010},Light{100},Very Heavy{001} 应该有秩序感 非常重 > 重 > 轻,这意味着非常重应该编码为 {111},转换为 7(111 二进制 = 7 十进制)
我的目标是创建一个自动化程序,希望您能帮助我,它可以将任何小矩阵作为输入 然后询问用户这些列中的哪些列具有顺序感,然后将这些变量编码为二进制,例如它们在矩阵中排序
例如: 您在脚本中编写矩阵
程序问你这个:有多少列是序数? 你回答即2 然后程序会问你这两列在矩阵中的位置是什么 答案:即 2 和 3 然后问你这些值是什么 答案即非常重,重和轻 然后它以二进制“编码”所有矩阵,但如果矩阵共有 3 列,则第一列以二进制随机编码 但是 2 & 3 中的元素应该有顺序(这并不意味着将矩阵从大到小重新排列,而只是二进制值)
我对 matlab 还很陌生,但这是我能想到的
clear all
clc
mat=[
{'Bleu'} {'Lourd'} {'Rond'};
{'Blanc'} {'Léger'} {'Carre'};
{'Vert'} {'Léger'} {'Rond'};
{'Bleu'} {'Très Lourd'} {'Carre'};
{'Blanc'} {'Léger'} {'Rond'};
{'Vert'} {'Très Lourd'} {'Carre'};
{'Bleu'} {'Léger'} {'Rond'};
{'Blanc'} {'Très Lourd'} {'Carre'};
{'Vert'} {'Très Lourd'} {'Rond'};
{'Bleu'} {'Lourd'} {'Carre'}];
NomCol = {'Couleur','Poids','Forme'};
for i=1:size(mat,1)
NomLignes{i}=['V' '' num2str(i)];
end
sTable = array2table(mat,'VariableNames',NomCol,'RowNames',NomLignes)
codage=[];
y=0;
for i=1:size(mat,2)
v=mat(:,i);
un=unique(v);
code=eye(size(un,1));
codage2=[];
for i=1:size(v,1)
pos=strmatch(v(i,:),un);
b=code(pos,:);
codage2=[codage2;b];
end
codage=[codage,codage2];
end
codage
我可以毫无问题地用二进制编码所有变量
代码 =
0 1 0 1 0 0 0 1
1 0 0 0 1 0 1 0
0 0 1 0 1 0 0 1
0 1 0 0 0 1 1 0
1 0 0 0 1 0 0 1
0 0 1 0 0 1 1 0
0 1 0 0 1 0 0 1
1 0 0 0 0 1 1 0
0 0 1 0 0 1 0 1
0 1 0 1 0 0 1 0
我如何需要重、非常重和轻值对它们有顺序意义,所以非常重应该是 = 1 1 1 / 重 = 1 1 0 / 轻 = 1 0 0,因为逻辑上非常重(7十进制)>重(十进制6)
如果有任何给定的改进功能或方法我可以做到这一点,请告诉我
【问题讨论】: