错误:res(2):越界1(尺寸为1×1)

huangapple go评论45阅读模式
英文:

error: res(2): out of bound 1 (dimensions are 1x1)

问题

我不明白如何在Octave中解决这个问题。有一个主文件和一个名为“gauss”的函数,它使用高斯方法解决矩阵。x的结果存储在“res”数组中。在“gauss”函数的末尾,我想在屏幕上显示结果,但我得到了这个错误。

main.m

```matlab
n9=9;
n10=10;
A1 = [61, -82, 35, -48, -40, -43, -50, -1, -92;
      -39, -99, -75, -75, 26, -1, 42, 21, 10;
      85, -88, 76, 48, -8, 90, 94, -86, -24;
      28, 8, -31, -29, 94, 98, -95, 96, 50;
      86, -37, 12, 14, -99, -26, 33, 17, 88;
      24, -30, -51, -57, 14, -53, 58, 21, -92;
      7, -9, -43, 69, -82, 56, -30, 100, -44;
      24, 37, 98, -77, -55, 16, 41, 12, 46;
      81, 79, 8, -70, 65, -76, -39, -82, -54]

A2 = [268, 74, -71, 11, -104, 16, -33, 64, 7, 11;
      74, 375, -118, 230, -51, -212, 180, 204, 163, -35;
      -71, -118, 400, -122, -81, 72, -201, 110, 20, 333;
      11, 230, -122, 239, -47, -40, 189, 188, 57, -69;
      -104, -51, -81, -47, 325, -53, -13, -222, -27, -118;
      16, -212, 72, -40, -53, 335, -64, -60, -110, 73;
      -33, 180, -201, 189, -13, -64, 325, 47, -8, -84;
      64, 204, 110, 188, -222, -60, 47, 446, 123, 123;
      7, 163, 20, 57, -27, -110, -8, 123, 379, 65;
      11, -35, 333, -69, -118, 73, -84, 123, 65, 378]

A3 = [-327, 106, 0, 0, 0, 0, 0, 0, 0, 0;
      -104, -512, 92, 0, 0, 0, 0, 0, 0, 0;
      0, -5, 56, 6, 0, 0, 0, 0, 0, 0;
      0, 0, 42, 501, 26, 0, 0, 0, 0, 0;
      0, 0, 0, 2, 167, -70, 0, 0, 0, 0;
      0, 0, 0, 0, 218, 439, -64, 0, 0, 0;
      0, 0, 0, 0, 0, -145, 943, 365, 0, 0;
      0, 0, 0, 0, 0, 0, -8, -91, -26, 0;
      0, 0, 0, 0, 0, 0, 0, -143, -749, 169;
      0, 0, 0, 0, 0, 0, 0, 0, 219, 566]
b1 = [67, 94, -83, 76, -93, -79, -18, 94, -77]
b2 = [420, 19, 384, 83, 108, 264, 80, 14, -215, 394]
b3 = [-981, 687, 845, 542, -915, -244, 409, 459, -552, -462]

gauss(A1, b1, n9)

gauss.m

function result = gauss(a, b, n)
  res = zeros(9, 1);
  temp = 0;
  for k = 1:n,
    for i = k+1:n,
      for j = k:n,
        if j == k,
          temp = a(i,j) / a(k,j);
          a(i,j) = a(i,j) - a(k,j) * a(i,j) / a(k,j);
          b(i) = b(i) - b(k) * temp;
        else
          a(i,j) = a(i,j) - temp * a(k,j);
        endif;
      end;
    end;
  end;
  for i = n:-1:1,
    for j = n-1:-1:1,
      if a(i, j) != 0,
        if i == j,
           res(i) = b(j) / a(i,j);
        else
           b(i) = b(i) - a(i,j) * res(j);
        endif;
      endif;
    end;
  end;

  for i = 1:9,
    printf("%f", res(i))
  end;

endfunction

<details>
<summary>英文:</summary>

I don&#39;t understand how to solve the problem in octave. There are a main file and a `gauss` function that solves the matrix using the Gaussian method. The x results are stored in the &quot;res&quot; array. At the end of the `gauss` function, I want to display the result on the screen, but I get this error.

main.m

n9=9;
n10=10;
A1 = [61, -82, 35, -48, -40, -43, -50, -1, -92;
-39, -99, -75, -75, 26, -1, 42, 21, 10;
85, -88, 76, 48, -8, 90, 94, -86, -24;
28, 8, -31, -29, 94, 98, -95, 96, 50;
86, -37, 12, 14, -99, -26, 33, 17, 88;
24, -30, -51, -57, 14, -53, 58, 21, -92;
7, -9, -43, 69, -82, 56, -30, 100, -44;
24, 37, 98, -77, -55, 16, 41, 12, 46;
81, 79, 8, -70, 65, -76, -39, -82, -54]

A2 = [268, 74, -71, 11, -104, 16, -33, 64, 7, 11;
74, 375, -118, 230, -51, -212, 180, 204, 163, -35;
-71, -118, 400, -122, -81, 72, -201, 110, 20, 333;
11, 230, -122, 239, -47, -40, 189, 188, 57, -69;
-104, -51, -81, -47, 325, -53, -13, -222, -27, -118;
16, -212, 72, -40, -53, 335, -64, -60, -110, 73;
-33, 180, -201, 189, -13, -64, 325, 47, -8, -84;
64, 204, 110, 188, -222, -60, 47, 446, 123, 123;
7, 163, 20, 57, -27, -110, -8, 123, 379, 65;
11, -35, 333, -69, -118, 73, -84, 123, 65, 378]

A3 = [-327, 106, 0, 0, 0, 0, 0, 0, 0, 0;
-104, -512, 92, 0, 0, 0, 0, 0, 0, 0;
0, -5, 56, 6, 0, 0, 0, 0, 0, 0;
0, 0, 42, 501, 26, 0, 0, 0, 0, 0;
0, 0, 0, 2, 167, -70, 0, 0, 0, 0;
0, 0, 0, 0, 218, 439, -64, 0, 0, 0;
0, 0, 0, 0, 0, -145, 943, 365, 0, 0;
0, 0, 0, 0, 0, 0, -8, -91, -26, 0;
0, 0, 0, 0, 0, 0, 0, -143, -749, 169;
0, 0, 0, 0, 0, 0, 0, 0, 219, 566]
b1 = [67, 94, -83, 76, -93, -79, -18, 94, -77]
b2 = [420, 19, 384, 83, 108, 264, 80, 14, -215, 394]
b3 = [-981, 687, 845, 542, -915, -244, 409, 459, -552, -462]

gauss(A1, b1, n9)


gauss.m

function result = gauss (a, b, n)
res = [9];
temp = 0;
for k = 1:n,
for i = k+1:n,
for j = k:n,
if j == k,
temp = a(i,j) ./ a(k,j);
a(i,j) = a(i,j) - a(k,j) .* a(i,j) ./ a(k,j);
b(i) = b(i) - b(k) * temp;
else
a(i,j) = a(i,j) - temp .* a(k,j);
endif;
end;
end;
end;
for i = n:1,
for j = n-1:0,
if a(i, j) != 0,
if i == j,
res = b(j) ./ a(i,j);
else
b(i) = b(i) - a(i,j) .* res(j);
endif;
endif;
end;
end;

for i = 1:9,
printf("%f", res(i))
end;

endfunction




I have tried declaring the &quot;res&quot; array in different ways

</details>


# 答案1
**得分**: 1

你应该始终复制/粘贴完整的错误消息,包括行号等,这样它会准确地告诉我们Octave正在告诉你什么。

我的猜测:在`gauss.m`中,你有一行代码:

`b(i) = b(i) - a(i,j) .* res(j);`。

你定义`res`为1x1标量(`res = [9]`)。在上面的那行代码中,你调用了`res(j)`。如果`res`只有一个元素,那么如果你要求它超过1个元素,就会出现错误(没有`res(2)`,这就是你在这个问题的标题中提到的错误消息试图告诉你的)。因为这是一个递增的for循环,你传入`n = 9`,`j`循环从`j = n-1:0`开始,可能会产生许多可能导致错误的`j`值。现在我看到`res`可以改变的地方。如果你期望它使`res`变大,在`res(2)`调用发生之前,似乎它没有这样做。

此外,没有深入研究代码在做什么的情况下,你的for循环中`j`的范围是从0开始的。Octave的索引从1开始。因此,任何时候你调用`res(0)`,都会引发错误。你应该确保这不会发生。

<details>
<summary>英文:</summary>

you should always copy/paste the full error message including line numbers, etc., so it tells us exactly what octave is telling you.  

my guess: in `gauss.m` you  have a line saying:

`b(i) = b(i) - a(i,j) .* res(j);`.  

You define `res` as a 1x1 scalar (`res = [9]`).  In that line above, you call for `res(j)`. If res only has 1 element, there will be an error if you ask it for more than 1 element. (there is no `res(2)`, which is what your error message you gave in the title to this question is trying to tell you.)  Because this is an incrementing for loop, you pass in `n = 9`, the `j` loop goes from `j = n-1:0`, there will be many possible values of j that could produce errors.  Now I see that there are places that `res` can change.  If you expect that to make `res` larger, before the `res(2)` call happens, it appears that it did not do so.  

Also, without looking too deeply into what the code is doing, your for loop has `j` going to 0. Octave indexing starts at 1.  So any time you call res(0) you&#39;re going to get an error. You should ensure that doesn&#39;t happen.

</details>



huangapple
  • 本文由 发表于 2023年1月9日 01:11:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定