简体   繁体   English

Octave :我收到一个尺寸错误:cat:执行 cell2mat 时尺寸不匹配

[英]Octave : I receive a dimension error: cat: dimension mismatch when doing cell2mat

I have a cell array (class cell) with dimension that I try to convert to doubles.我有一个具有维度的元胞数组(类元胞),我尝试将其转换为双精度数。 I get a dimension error when I use cell2mat.使用 cell2mat 时出现尺寸错误。 (row 1 and kol 1 are not numeric) (第 1 行和 kol 1 不是数字)

debug> `class(mycsvdata)`
ans = cell

CCC=cell2mat(mycsvdata(2:end,2:end))
error: cat: dimension mismatch
error: called from
cell2mat at line 80 column 11
leesCsvPuur at line 7 column 2
verwerkStooq at line 37 column 8
handleStooq at line 77 column 1
testinvoer03 at line 72 column 1

debug> `size(mycsvdata)`
ans =
   9   7

I tried cell2mat :我试过 cell2mat :

debug> `cell2mat{1 2 3; 4 5 6}`
ans =
   1   2   3
   4   5   6

I would appreciate any advice.我将不胜感激任何建议。

There's a lot of information missing here, but I suspect your problem boils down to the following three facts.这里缺少很多信息,但我怀疑您的问题归结为以下三个事实。

  1. A standard array must necessarily always contain elements of the same type .标准数组必须始终包含相同类型的元素。 It cannot, eg contain a 'string' in one index, and a 'number' in another;例如,它不能在一个索引中包含一个“字符串”,而在另一个索引中包含一个“数字”; that's what cell arrays are for.这就是元胞数组的用途。

  2. Attempting to convert a cell array which contains such mixed elements into a standard array via cell2mat should, depending on your warning / error levels, either fail, or convert all elements to their least common denominator.尝试通过 cell2mat 将包含此类混合元素的元胞数组转换为标准数组时,应根据您的警告/错误级别,要么失败,要么将所有元素转换为它们的最小公分母。 Eg if you have both 'date strings' (like '2011-01-02') and 'numbers', it will probably interpet the 'numbers' as ascii character codes first.例如,如果您同时拥有“日期字符串”(如“2011-01-02”)和“数字”,则它可能会首先将“数字”插入为 ascii 字符代码。

  3. Strings in octave are simply arrays of characters.八度中的字符串只是字符数组。 If you try to vertically concatenate two strings which are of unequal length, you will get an error about mismatched dimensions, since the resulting array in a concatenation always needs to be properly rectangular.如果您尝试垂直连接两个长度不等的字符串,您将收到关于维度不匹配的错误,因为连接中的结果数组始终需要正确为矩形。

Eg if all strings are same size (note numeric conversion)例如,如果所有字符串的大小相同(注意数字转换)

> c = { '2011-01-01', 98.1; '2011-01-20', 97 };
> C = cell2mat(c)
warning: implicit conversion from numeric to char
  C =
    2011-01-01b
    2011-01-20a

If they're not (note the unpadded date):如果不是(请注意未填充的日期):

> c = { '2011-01-01'; '2011-01-2' };
> C = cell2mat(c)
error: cat: dimension mismatch
error: called from
    cell2mat at line 80 column 11

I don't know the exact nature of the cell array resulting from your csv2cell operation, but I would wager something similar to the above is going on.我不知道由您的csv2cell操作产生的单元格数组的确切性质,但我敢打赌,与上述类似的事情正在发生。

In general, it is probably not the right approach to convert via cell2mat immediately.通常,立即通过 cell2mat 进行转换可能不是正确的方法。 Instead, you could probably collect all columns to separate variables, and then treat them accordingly, either via cell2mat or via a cellfun approach.相反,您可以收集所有列来分隔变量,然后通过cell2matcellfun方法相应地处理它们。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM