Matlab 将矩阵转换为二进制值

Matlab 将矩阵转换为二进制值,matlab,matrix,binary,Matlab,Matrix,Binary,假设我有一个当前矩阵: mat=[ {'Blue'} {'Heavy'} {'Rounded'}; {'White'} {'Light'} {'Square'}; {'Green'} {'Light'} {'Rounded'}; {'Blue'} {'Very Heavy'} {'Square'}; {'White'} {'Light'} {'Rounded'}; {'Green'} {'Very Heavy'} {'Square'}; {

假设我有一个当前矩阵:

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},正如你所看到的,在这种情况下没有秩序感 我们不能说蓝色>白色或蓝色 然而,对于列Weight=>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)


请告诉我是否有任何给定的优化函数或方法,我可以这样做

转换矩阵很简单:

% convert cell-matrix to table with specific column names
T = cell2table(mat,'VariableNames',{'Color','Weight','Form'});
自动生成二进制键有点困难,但是matlab可以帮助您使用函数
de2bi

% assuming that this is your user-input
InptKey = {'Blue','Green','White'};
% create binary keys
num_bin = de2bi(1:length(InptKey));
现在,棘手的部分确实是检查

% allocate remporary variable
tmp = cast(zeros(height(T),size(num_bin,2)),class(num_bin));
% or simply use
tmp = false(height(T),size(num_bin,2));

% loop through rows
for i = 1:length(Inptkey)
    key = InptKey{i};
    val = num_bin(i,:);

    lg = strcmpi(T.Color,key);
    % depending on the Matlab version your are working with the first
    % option also works, see 'implicit expansion'. Otherwise you need 
% to expand "val" manually
%     tmp(lg,:) = val;
    tmp(lg,:) = repmat(val,sum(lg),1);
end
% assign 'tmp' variable (you need to assign the whole vector at once not to
% cause a conversion conflict from cell to logical)
T.Color = tmp;
请注意,不能直接用二进制向量替换字符串,因为它们的类型不同(单元与逻辑),因此需要使用临时向量