简体   繁体   English

为什么在尝试使用vertcat时出现错误?

[英]Why am I getting an error when trying to use vertcat?

I'm getting an error when trying to use vertcat to concatenate two matrices. 尝试使用vertcat连接两个矩阵时出现错误。 How can I concatenate this matrix 如何连接此矩阵

text = 

  Columns 1 through 6

    'GL/GG'    'go'    'vc (m/min)'    'fz (mm)'    'Fx (N)'    'Fy (N)'
    '[]'       '[]'    '[]'            '[]'         '[]'        '[]'    

  Columns 7 through 9

    'd (°)'    'MA (%)'    'V (%)'
    '[]'       '[]'        '[]' 

which is 2-by-9, with another matrix that includes double values and is 96-by-9. 它是2×9,另一个矩阵包含双精度值,是96×9。

I don't understand why there is a problem with this vertcat function. 我不明白为什么此vertcat函数有问题。 As far as I can tell, c = vertcat(text,text) works fine: 据我所知, c = vertcat(text,text)可以正常工作:

 Columns 1 through 6

    'GL/GG'    'go'    'vc (m/min)'    'fz (mm)'    'Fx (N)'    'Fy (N)'
    '[]'       '[]'    '[]'            '[]'         '[]'        '[]'    
    'GL/GG'    'go'    'vc (m/min)'    'fz (mm)'    'Fx (N)'    'Fy (N)'
    '[]'       '[]'    '[]'            '[]'         '[]'        '[]'    

  Columns 7 through 9

    'd (°)'    'MA (%)'    'V (%)'
    '[]'       '[]'        '[]'   
    'd (°)'    'MA (%)'    'V (%)'
    '[]'       '[]'        '[]'   

What should I do? 我该怎么办?

Columns 1 through 6 第1至6栏

        1            0            1            0      0.65318     0.045977
        1          0.2      0.42857            1      0.46243      0.21264
        0            0      0.42857          0.4     0.023121      0.26437
        0          0.2      0.14286          0.4      0.10405      0.41954
        0          0.2            1          0.4      0.14451       0.4023
        0          0.6            0            1     0.034682       0.2931
        0            1      0.42857          0.4      0.21387      0.48276
        1          0.2            1            0      0.53179     0.086207
        1          0.6            1            0      0.46243     0.011494
        1          0.6      0.14286            1      0.62428      0.52299
        1            1      0.42857            0            1      0.21839
        0          0.2      0.42857            1     0.028902      0.52874
        0          0.6            0            0      0.16185      0.16667
        0          0.6      0.14286            0     0.098266      0.13218
        0          0.6            1            0      0.19653      0.14368
        0          0.6            1          0.4      0.10983      0.40805
        0            1            0            0      0.25434      0.25862
        0            1            1            1     0.046243      0.47701
        1            0      0.14286          0.4       0.7052       0.3046
        1          0.2            1            1      0.66474      0.32184
        1          0.6            1          0.4      0.54913     0.057471
        1          0.6            1            1      0.89017      0.24713
        1            1      0.14286            0      0.74566      0.15517
        1            1      0.14286          0.4      0.42197     0.091954
        1            1            1            1      0.61272      0.14943
        0            0            1            1     0.069364       0.7931
        0            1      0.14286            0      0.33526      0.37931
        0            1      0.14286            1     0.034682      0.55172
        1            0      0.42857            0      0.56069      0.17816
        1          0.6            0            1      0.57803      0.46552
        1            1            1            0      0.87861     0.057471
        1            0            0          0.4      0.45087      0.18966
        1          0.2            0            0       0.3526     0.028736
        1          0.2            0          0.4      0.50867      0.29885
        1          0.2            1          0.4       0.7341      0.32184
        1          0.2            0            1      0.56647       0.2931
        1          0.6      0.14286            0      0.43931     0.086207
        1          0.6      0.42857          0.4      0.39306      0.21839
        1            1      0.42857          0.4      0.50289       0.1092
        0            0      0.42857            0      0.21965      0.32184
        0            0      0.14286          0.4      0.17919      0.74138
        0            0            1          0.4     0.098266      0.47126
        0          0.2            1            0      0.33526      0.36782
        0          0.2      0.14286            1     0.023121      0.47701
        0          0.6      0.14286          0.4      0.16185      0.54598
        0            1      0.14286          0.4     0.023121      0.20115
        1          0.2      0.14286            0      0.43931     0.068966
        1          0.2      0.14286          0.4      0.52023      0.25862
        1          0.6      0.42857            0      0.43353     0.034483
        1          0.6            0          0.4      0.48555      0.26437
        1            1            0          0.4      0.42197      0.16092
        1            1            0            1      0.80925       0.6092
        0            0            0            0      0.23121      0.37931
        0            0            1            0      0.30636       0.1954
        0          0.2      0.42857            0      0.18497      0.15517
        0          0.6      0.42857            1            0      0.53448
        1            0            1          0.4      0.54913      0.13218
        1          0.6      0.14286          0.4      0.45087      0.27586
        1          0.6      0.42857            1      0.52601      0.40805
        1            1      0.42857            1      0.83237      0.55747
        0            0            0            1     0.017341      0.50575
        0            0      0.42857            1     0.080925            1
        0          0.2            0            0      0.11561      0.15517
        0          0.6      0.42857          0.4     0.052023      0.36782
        0          0.6            1            1      0.10983      0.41954
        0            1            1            0      0.40462      0.27586
        0            1      0.42857            1      0.14451      0.74713
        1            0            0            0      0.65318       0.1954
        1            0      0.42857          0.4      0.55491      0.24138
        1            0      0.14286            1      0.99422      0.61494
        1          0.6            0            0       0.3526            0
        0            0            0          0.4     0.092486      0.26437
        0          0.2            0            1     0.023121      0.38506
        0          0.6      0.14286            1     0.017341      0.58046
        0            1      0.42857            0      0.49711      0.41954
        0            1            0            1      0.15029      0.72414
        1            0      0.42857            1      0.78035      0.62069
        1            0            1            1      0.72832      0.44828
        1            1      0.14286            1      0.81503      0.27586
        0            0      0.14286            0      0.21387      0.21839
        0            0      0.14286            1     0.086705       0.7931
        0          0.2      0.14286            0      0.10983       0.1092
        0          0.2            0          0.4     0.086705      0.41954
        0          0.2      0.42857          0.4     0.023121       0.1092
        0          0.6            0          0.4     0.052023      0.31609
        0            1            0          0.4     0.098266      0.17241
        1            0      0.14286            0      0.65896      0.16667
        1            0            0            1      0.90751      0.73563
        1          0.2      0.42857            0       0.4104     0.051724
        1          0.2      0.42857          0.4      0.36994      0.17241
        1          0.2      0.14286            1      0.47977      0.23563
        1            1            0            0       0.6185      0.16667
        1            1            1          0.4      0.39884      0.25287
        0          0.2            1            1     0.052023          0.5
        0          0.6      0.42857            0      0.18497      0.15517
        0            1            1          0.4      0.16763       0.3046

Columns 7 through 9 第7至9栏

 0.061728      0.26761        0.375
  0.32099            0            0
  0.91358      0.74648        0.375
  0.76543      0.35211       0.4375
  0.71605      0.39437      0.33333
  0.82716      0.67606      0.10417
  0.66667      0.49296      0.52083
  0.16049       0.4507      0.58333
  0.12346      0.22535       0.8125
  0.39506      0.87324       0.1875
 0.074074      0.19718      0.58333
  0.85185       0.5493       0.1875
  0.60494      0.57746        0.375
  0.69136      0.29577      0.47917
  0.54321       0.4507      0.52083
  0.75309      0.64789       0.1875
  0.54321      0.47887        0.625
  0.92593      0.15493     0.083333
  0.23457      0.19718      0.47917
  0.25926      0.49296       0.1875
  0.12346      0.77465      0.14583
  0.12346      0.59155      0.22917
  0.11111     0.056338            1
  0.23457      0.25352      0.33333
  0.17284      0.39437      0.66667
  0.82716      0.94366      0.29167
  0.53086      0.26761        0.375
  0.83951      0.59155     0.083333
  0.22222      0.22535      0.66667
  0.39506      0.38028      0.10417
        0      0.42254      0.72917
  0.30864      0.71831      0.22917
  0.23457      0.47887        0.375
  0.34568      0.16901      0.22917
  0.23457      0.16901       0.1875
   0.2963      0.59155      0.10417
  0.22222      0.25352        0.375
  0.38272      0.39437        0.375
  0.19753      0.47887       0.1875
  0.60494      0.64789      0.52083
  0.75309      0.49296      0.47917
  0.77778      0.69014        0.375
  0.51852      0.39437      0.70833
  0.85185      0.84507      0.14583
   0.7284      0.64789      0.29167
  0.82716      0.16901      0.29167
  0.19753      0.39437       0.5625
  0.30864      0.38028      0.33333
  0.17284      0.35211       0.4375
  0.34568      0.12676      0.29167
  0.30864       0.5493      0.22917
  0.34568      0.59155      0.22917
  0.61728      0.52113       0.4375
  0.44444      0.32394        0.375
   0.5679      0.22535        0.375
  0.87654      0.29577        0.375
  0.19753      0.59155     0.083333
  0.37037      0.80282      0.29167
  0.39506      0.15493      0.33333
  0.30864      0.42254      0.14583
  0.90123      0.59155     0.083333
  0.82716      0.94366      0.22917
  0.66667      0.47887      0.39583
  0.81481      0.67606      0.33333
        1      0.57746      0.33333
  0.40741      0.29577      0.29167
  0.77778      0.74648      0.39583
  0.18519      0.39437      0.70833
   0.2716      0.52113      0.29167
   0.2716      0.25352      0.22917
  0.19753      0.39437      0.29167
  0.74074       0.4507      0.52083
  0.83951      0.61972       0.1875
  0.85185      0.91549      0.33333
  0.41975      0.29577        0.625
  0.76543      0.64789      0.22917
  0.35802      0.64789      0.22917
  0.30864      0.52113       0.1875
  0.17284      0.90141      0.10417
   0.5679      0.47887      0.52083
  0.81481      0.84507        0.375
  0.65432      0.49296      0.29167
  0.77778      0.57746      0.22917
  0.93827      0.49296      0.22917
  0.80247      0.29577         0.25
   0.7037      0.57746      0.33333
  0.16049      0.15493      0.58333
  0.34568      0.84507      0.10417
  0.20988      0.61972      0.29167
  0.35802      0.42254        0.375
  0.32099      0.52113     0.083333
  0.18519       0.4507      0.58333
  0.39506      0.47887         0.25
  0.92593            1      0.33333
   0.5679      0.22535      0.39583
  0.66667      0.19718       0.1875

You can only concatenate vertical arrays (of type string) if they are the equal size ie the rows and columns must be identical. 如果垂直数组的大小相等,即行和列必须相同,则只能将它们连接起来。 A similar question was answer here - https://math.stackexchange.com/questions/449791/why-do-i-keep-getting-a-vertcat-error-in-matlab . 在这里回答了一个类似的问题-https: //math.stackexchange.com/questions/449791/why-do-i-keep-getting-a-vertcat-error-in-matlab May be this will help you understand what you are up against? 也许这可以帮助您了解自己所面对的问题?

A tip for you would be to pad the difference using white-space " "? 给您的提示是使用空格“”来填充差异? This will make sure that you have an equal size array to work with. 这将确保您具有相等大小的数组。 HOwever, I don't know the parameters of your operation, so using " " as a padding element may affect what you are trying to achieve. 但是,我不知道您的操作参数,因此将“”用作填充元素可能会影响您要实现的目标。

The problem is that your text matrix is in fact a cell array where as your matrix of doubles is just a regular double matrix. 问题在于您的text矩阵实际上是一个单元格数组 ,其中作为双精度矩阵的矩阵只是一个常规双精度矩阵。 In order to do the concatenation you first need to convert your array of doubles to a cell array where each element is in a separate cell (ie a 96 -by- 9 cell array). 为了进行串联,首先需要将双精度数组转换为单元格数组,其中每个元素都位于单独的单元格中(即96 x 9单元格数组)。 To do this you use mat2cell : 为此,您可以使用mat2cell

text = { 'GL/GG', 'go', 'vc (m/min)', 'fz (mm)', 'Fx (N)', 'Fy (N)', 'd (°)', 'MA (%)', 'V (%)';
         '[]',    '[]', '[]',         '[]',      '[]',     '[]',     '[]',    '[]',     '[]'};
num = rand(96,9);

vertcat(text, mat2cell(num, ones(size(num,1),1), ones(1,size(num,2))));

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

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