Matlab"使用vertcat错误"

时间:2015-09-09 03:49:38

标签: matlab

我试图在Matlab中创建一个表但是我一直收到一个错误,上面写着"连接的矩阵的维数不一致。"

这是我的代码的相关部分:

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)))];

问题在于col2的第一行。有人可以帮忙吗?我一直在试图从mathworks网站上学习,但每当我尝试以不同的方式格式化表格时,我会遇到新的错误。我不明白为什么我会遇到问题,因为每列有16行。

1 个答案:

答案 0 :(得分:1)

如果这是你得到的同样错误......

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)))

...这只是因为对abs的最后四组调用是按自己的行设置的,而不是col2赋值的一部分。

以下执行没有问题。让事情变得更容易,并尽可能地进行矢量化!

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
相关问题