简体   繁体   English

Matlab“使用vertcat时出错”

[英]Matlab “Error using vertcat”

I'm trying to make a table in Matlab but I keep getting an error that says "Dimensions of matrices being concatenated are not consistent." 我正在尝试在Matlab中制作表格,但始终出现错误,提示“被连接的矩阵维不一致。”

Here's the relevant parts of my code: 这是我的代码的相关部分:

x0 = 1.2;
fp = cos(1.2);
fwd = @(x0, h)(sin(x0+h)-sin(x0))./h; %forward differential formula
h1 = @(k)(10.^-k);
h = [h1(1);h1(2);h1(3);h1(4);h1(5);h1(6);h1(7);h1(8);h1(9);h1(10);h1(11);h1(12);h1(13);h1(14);h1(15);h1(16)];
col2 = [abs(fp-fwd(x0,h(1))), abs(fp-fwd(x0,h(2))), abs(fp-fwd(x0,h(3))), abs(fp-fwd(x0,h(4)))
abs(fp-fwd(x0,h(5))), abs(fp-fwd(x0,h(6))), abs(fp-fwd(x0,h(7)))
abs(fp-fwd(x0,h(8))), abs(fp-fwd(x0,h(9))), abs(fp-fwd(x0,h(10)))
abs(fp-fwd(x0,h(11))), abs(fp-fwd(x0,h(12))), abs(fp-fwd(x0,h(13)))
abs(fp-fwd(x0,h(14))), abs(fp-fwd(x0,h(15))), abs(fp-fwd(x0,h(16)))];

The issue is with the first line of col2. 问题出在col2的第一行。 Can someone please help here? 有人可以帮忙吗? I've been beating my brains out trying to learn from the mathworks website, but every time I try to format the table a different way I run into new errors. 我一直在竭尽全力尝试从mathworks网站学习,但是每次我尝试以另一种方式格式化表格时,都会遇到新的错误。 I don't understand why I'm having issues at all since each column will have 16 rows. 我不明白为什么我会遇到问题,因为每列将有16行。

If this is the same error you're getting... 如果这是相同的错误,您将得到...

Error using vertcat
CAT arguments dimensions are not consistent.

Error in vercatError (line 17)
col2 = [abs(fp-fwd(x0,h(1))), abs(fp-fwd(x0,h(2))), abs(fp-fwd(x0,h(3))),
abs(fp-fwd(x0,h(4)))

...it's only because the last four sets of calls to abs are set on their own lines instead of being part of the assignment to col2 . ...这仅仅是因为对abs的最后四组调用是在自己的行上设置的,而不是属于col2的分配的一部分。

The following executes without issue. 以下执行没有问题。 Make things easier on yourself and vectorize whenever possible! 使事情变得更轻松,并尽可能进行矢量化处理!

x0 = 1.2;
fp = cos(1.2);
fwd = @(x0, h)(sin(x0+h)-sin(x0))./h;
h1 = @(k)(10.^-k);
h = h1(1:16)';
col2 = abs(fp-fwd(x0,h));

h =

   0.100000000000000
   0.010000000000000
   0.001000000000000
   0.000100000000000
   0.000010000000000
   0.000001000000000
   0.000000100000000
   0.000000010000000
   0.000000001000000
   0.000000000100000
   0.000000000010000
   0.000000000001000
   0.000000000000100
   0.000000000000010
   0.000000000000001
   0.000000000000000


col2 =

   0.047166759977007
   0.004666195860716
   0.000466079897112
   0.000046602557581
   0.000004660191334
   0.000000465968587
   0.000000046193262
   0.000000000436105
   0.000000055947256
   0.000000166969559
   0.000007938530731
   0.000130063063440
   0.000425048448873
   0.004015843649628
   0.081731455373389
   0.362357754476674

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

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