索引超出了matlab中的矩阵尺寸误差

时间:2015-07-08 12:59:10

标签: matlab

我收到此错误

  

指数超出矩阵维度

当我在MATLAB中运行以下内容时。我做错了什么?

>> a

a =

     1     2     3
     4     5     6
     7     8     9

>> a(:)

ans =

     1
     4
     7
     2
     5
     8
     3
     6
     9


>> sum(a(:))

Index exceeds matrix dimensions.

>> sum(a(:),1)

Index exceeds matrix dimensions.

>> sum(a(:),2)

Index exceeds matrix dimensions.

1 个答案:

答案 0 :(得分:3)

您确实在名称sum

下设置了一个变量
a = [1 2 3 ; 4 5 6; 7 8 9]
sum = 1;


>> sum(a);
Index exceeds matrix dimensions.


clear sum;
>> sum(a)

ans =

    12    15    18