Cell2mat returns error: cat: dimension mismatch

Cell2mat returns error: cat: dimension mismatch

我有一个尺寸为 1311114x2 的大型元胞数组。我的数据摘录如下所示。

Rotation angle  Measured distance
-358,506    26,992
-358,758    26,993
-359,010    26,992
-359,262    26,993
-359,514    26,992
-359,766    26,992
-0,018  26,993
-0,270  26,991
-0,522  26,992
-0,774  26,992
-1,044  26,993
-1,296  26,992

为了创建我的输出变量,我使用了 cell2mat 函数

data = [cell2mat(raw(:,1)), cell2mat(raw(:,2))];

并收到以下错误消息。

error: cat: dimension mismatch
error: called from
    cell2mat at line 80 column 11
    Cloud_reconstruction at line 102 column 6

有人知道如何解决这个问题吗?

我假设 raw 元胞数组中的值是字符串(或字符数组)。由于这些字符串的长度各不相同,因此所需的串联将无法正常工作,正如错误消息中所述。

您可以使用 Octave 的 str2num function to convert strings to numerical values. That won't work out-of-the-box here, since you also have commas in your strings. It seems, these are decimal separators. You need to replace these commas, e.g. using Octave's strrep method. With the help of Octave's cellfun 方法,上述函数可以应用于元胞数组的所有条目。

这是我的解决方案:

raw = {
  '-358,506',    '26,992';
  '-358,758',    '26,993';
  '-359,010',    '26,992';
  '-0,018',      '26,993'
}

data = cellfun(@(x)str2num(x), strrep(raw, ',', '.'))

这导致:

  raw =
  {
    [1,1] = -358,506
    [2,1] = -358,758
    [3,1] = -359,010
    [4,1] = -0,018
    [1,2] = 26,992
    [2,2] = 26,993
    [3,2] = 26,992
    [4,2] = 26,993
  }

  data =

    -358.506000    26.992000
    -358.758000    26.993000
    -359.010000    26.992000
      -0.018000    26.993000

希望对您有所帮助!