简体   繁体   English

使用带有字符的数组时,要级联的矩阵的维数不一致

[英]Dimensions of matrices being concatenated are not consistent using array with characters

I'm trying to initialize 我正在尝试初始化

labels =['dh';'Dh';'gj';'Gj';'ll';'Ll';'nj';'Nj';'rr';'Rr';'sh';'Sh';'th';'Th';'xh';'Xh';'zh';'Zh';'ç';'Ç';'ë';'Ë'];

But it shows me the error on title.When I try with numbers it's all perfect but not with characters.What could be the problem? 但是它告诉我标题上的错误。当我尝试使用数字时,这是完美的,但对于字符来说不是。这可能是什么问题?

If you wish to eliminate any padding, you can also store it into a cell as follows. 如果您希望消除任何填充,也可以按以下方式将其存储到单元中。

labels = {'dh';'Dh';'gj';'Gj';
         'll';'Ll';'nj';'Nj';
         'rr';'Rr';'sh';'Sh';
         'th';'Th';'xh';'Xh';
         'zh';'Zh';'ç';'Ç';
         'ë';'Ë'};

Then you can reference the "i"th element using labels{i} instead of labels(i,:) which is simpler. 然后,您可以使用labels{i}而不是更简单的labels(i,:)来引用第“ i”个元素。 You can further run more string operations using cellfun and not interfere with any existing values that you've stored. 您可以使用cellfun进一步运行更多的字符串操作,并且不会干扰已存储的任何现有值。

I agree with krisdestruction that using a cell array makes the code accessing the strings simpler and is generally more idiomatic. 我同意krisdestruction的观点 ,即使用单元格数组可使访问字符串的代码更简单,并且通常更惯用。 That is what I would also recommend unless there is a compelling reason to do something else. 除非有迫不得已的理由要做其他事情,否则我也建议这样做。

For completeness, you could use the char function to add the padding automatically for you if you really want a character array: 为了完整起见,如果您确实需要字符数组,可以使用char函数为您自动添加填充:

>> char('aa','bb','c')

ans =

aa
bb
c 

where the last row is 'c ' . 最后一行是'c ' From the char documentation: char文档中:

S = char(A1,...,AN) converts the arrays A1,...,AN into a single character array. S = char(A1,...,AN)将数组A1,...,AN转换为单个字符数组。 After conversion to characters, the input arrays become rows in S. Each row is automatically padded with blanks as needed . 转换为字符后,输入数组成为S中的根据需要,每行将自动用空白填充 An empty string becomes a row of blanks. 空字符串变成一行空白。

(Emphasis mine) (强调我的)

From the Mathworks documentation : Mathworks文档中

Apply the MATLAB concatenation operator, []. 应用MATLAB串联运算符[]。 Separate each row with a semicolon (;). 用分号(;)分隔每行。 Each row must contain the same number of characters . 每行必须包含相同数量的字符 For example, combine three strings of equal length: 例如,组合三个等长的字符串:

You can try padding like this to make every row 2 characters: 您可以尝试这样填充以使每行2个字符:

labels = ['dh';'Dh';'gj';'Gj';
         'll';'Ll';'nj';'Nj';
         'rr';'Rr';'sh';'Sh';
         'th';'Th';'xh';'Xh';
         'zh';'Zh';'ç ';'Ç ';
         'ë ';'Ë '];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用垂直时发生错误:串联的矩阵尺寸不一致 - Error using vertical: Dimensions of matrices being concatenated are not consistent 如何解决:使用horzcat时发生错误所连接矩阵的维数不一致 - How to solve: Error using horzcat Dimensions of matrices being concatenated are not consistent 使用CAT的matlab错误,所连接矩阵的维数不一致 - matlab error using CAT, Dimensions of matrices being concatenated are not consistent bsxfun:所连接矩阵的维数不一致 - bsxfun: Dimensions of matrices being concatenated are not consistent matlab连接的矩阵尺寸不一致 - matlab Dimensions of matrices being concatenated are not consistent 我如何处理“使用vertcat的错误连接矩阵的尺寸在Matlab中不一致”? - How do i handle “Error using vertcat Dimensions of matrices being concatenated are not consistent” in Matlab? 我的代码运行,有时会出现错误“使用horzcat时出错,所连接的矩阵维数不一致。” - My code runs then sometimes it has error “Error using horzcat Dimensions of matrices being concatenated are not consistent.” 使用 horzcat 时出错 连接的 arrays 的维度不一致。 Matlab - Error using horzcat Dimensions of arrays being concatenated are not consistent. Matlab 在1x6802 double和6802x1 double数据矩阵中,级联矩阵的尺寸不一致 - Dimensions of matrices being concatenated are not consistent in 1x6802 double and 6802x1 double data matrix sequenceInputLayer() 被连接的数组的维度不一致 - sequenceInputLayer() Dimensions of arrays being concatenated are not consistent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM