【问题标题】:Transform Matrix to Binary values将矩阵转换为二进制值
【发布时间】: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)

如果有任何给定的改进功能或方法我可以做到这一点,请告诉我

【问题讨论】:

    标签: matlab matrix binary


    【解决方案1】:

    转换矩阵很简单:

    % 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;
    

    请注意,您不能直接用二进制向量替换字符串,因为它们的类型不同(cell vs logical),因此您需要使用临时向量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-06
      • 2022-01-23
      • 2015-06-01
      • 1970-01-01
      • 2019-05-02
      • 2015-05-13
      • 2012-12-03
      • 1970-01-01
      相关资源
      最近更新 更多