簡體   English   中英

Matlab“使用vertcat時出錯”

[英]Matlab “Error using vertcat”

我正在嘗試在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行。

如果這是相同的錯誤,您將得到...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM