String Matlab中用于分类的标签数据
我有两组不同大小的序列数据,String Matlab中用于分类的标签数据,string,matlab,matrix,char,cell-array,String,Matlab,Matrix,Char,Cell Array,我有两组不同大小的序列数据,A和B,我想用它们来训练分类器,我在两个字符变量中有两个标签,比如 L1 = 'label A'; L2 = 'label B'; 如何制作合适的标签 我将使用cat(1,A,B)首先合并数据 根据大小(A,1)和大小(B,1),它应该类似于 label = ['label A' 'label A' 'label A' . . 'label B' 'labe
A
和B
,我想用它们来训练分类器,我在两个字符变量中有两个标签,比如
L1 = 'label A';
L2 = 'label B';
如何制作合适的标签
我将使用cat(1,A,B)代码>首先合并数据
根据大小(A,1)
和大小(B,1)
,它应该类似于
label = ['label A'
'label A'
'label A'
.
.
'label B'
'label B'];
这将创建您要查找的标签矩阵。你需要使用。Repmat可帮助您多次重复某个值如果标签名称具有相同的长度,您可以创建如下数组:
L = [repmat(L1,size(A,1),1);repmat(L2,size(B,1),1)];
否则,您需要使用单元格数组:
L = [repmat({L1},size(A,1),1);repmat({L2},size(B,1),1)];
假设:
na = size(A,1);
nb = size(B,1);
以下是创建标签单元格数组的几种方法:
雷普马特
labels = [repmat({'label A'},na,1); repmat({'label B'},nb,1)];
细胞阵列填充
labels = cell(na+nb,1);
labels(1:na) = {'label A'};
labels(na+1:end) = {'label B'};
单元阵列线性索引
labels = {'label A'; 'label B'};
labels = labels([1*ones(na,1); 2*ones(nb,1)]);
单元阵列线性索引(另一个)
num2str
labels = cellstr(num2str((1:(na+nb) > na).' + 'A', 'label %c'));
strcat
idx = [1*ones(na,1); 2*ones(nb,1)];
labels = strcat({'label '}, char(idx+'A'-1));
。。。你明白了:)
请注意,在字符串单元格数组和字符矩阵之间转换总是很容易的:
% from cell-array of strings to a char matrix
clabels = char(labels);
% from a char matrix to a cell-array of strings
labels = cellstr(clabels);
谢谢,我知道这不会很难,我只是找不到复制字符的方法,但正如你所说的,repmat
是答案。让我们看看其他人是否能想出更多的方法:)有点疯狂,但也可能是labels=cell(na+nb,1)代码>[labels{1:na}]=deal('labela')
和[labels{na+1:end}]=deal('labelb')
@JandeGier:一点也不疯狂,这是展示deal
功能的好方法
idx = [1*ones(na,1); 2*ones(nb,1)];
labels = strcat({'label '}, char(idx+'A'-1));
% from cell-array of strings to a char matrix
clabels = char(labels);
% from a char matrix to a cell-array of strings
labels = cellstr(clabels);