[英]Subscripted assignment dimension mismatch
So, I'm trying to do the Gauss-Seidel method in Matlab and I found a code that does this but when I apply it to my matrices I get the Subscripted assignment dimension mismatch.
因此,我尝试在Matlab中执行Gauss-Seidel方法,并找到了执行此操作的代码,但是当将其应用于矩阵时,出现下
Subscripted assignment dimension mismatch.
error. 错误。 I will show you my code in order to get a better idea.
我将向您展示我的代码,以获得更好的主意。
%size of the matrix
n = 10;
%my matrices are empty in the beginning because my professor wants to run the algorithm for n = 100
and n = 1000. A's diagonal values are 3 and every other value is -1. b has the constants and the
first and last value will be 2,while every other value will be 1.
A = [];
b = [];
%assign the values to my matrices
for i=1:n
for j=1:n
if i == j
A(i,j) = 3;
else
A(i,j) = -1;
end
end
end
for i=2:n-1
b(i) = 1;
end
%here is the Gauss-Seidel algorithm
idx = 0;
while max(error) > 0.5 * 10^(-4)
idx = idx + 1;
Z = X;
for i = 1:n
j = 1:n; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i) = (b(i) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xsolution(:,idx) = X;
error = abs(X - Z);
end
GaussSeidelTable = [1:idx;Xsolution]'
MaTrIx = [A X b]
I get the error for the Xsolution(:,idx) = X;
我得到
Xsolution(:,idx) = X;
part. 部分。 I don't know what else to do.
我不知道该怎么办 The code posted online works though, and the only difference is that the matrices are hardcoded in the m-file and A is a 5x5 matrix while b is a 5x1 matrix.
在线发布的代码虽然有效,但唯一的区别是矩阵在m文件中进行了硬编码,A是5x5矩阵,而b是5x1矩阵。
I am unable to run your code because some variables are not initialised, at least error
and X
. 我无法运行您的代码,因为某些变量尚未初始化,至少是
error
和X
I assume the Problem is caused because Xsolution
is already initialised from a previous run with a different size. 我认为是由于
Xsolution
已从以前的运行中使用不同的大小初始化过而导致出现问题。 Insert a Xsolution=[]
to fix this. 插入
Xsolution=[]
可以解决此问题。
Besides removing the error I have some suggestions to improve your code: 除了消除错误,我还有一些建议可以改善您的代码:
error
or i
. error
或i
。 error
is a build-in function to throw errors and i
is the imaginary unit. error
是一个内置函数,用于引发错误,而i
是虚数单位。 Both can cause hard to debug errors. A=-1*ones(n,n);A(eye(size(A))==1)=3;
A=-1*ones(n,n);A(eye(size(A))==1)=3;
初始化A A=-1*ones(n,n);A(eye(size(A))==1)=3;
, it's faster not to use a for
loop in this case. for
循环。 To initialise b
you can simply write b(1)=0;b(2:n-1)=1;
b
您可以简单地编写b(1)=0;b(2:n-1)=1;
the first time you run the code, Xsolution(:,idx) = X
will create a Xsolution
with the size of X
. 第一次运行代码时,
Xsolution(:,idx) = X
将创建一个大小为X
的Xsolution
。
the second time you run it, the existing Xsolution
does not fit the size of new X. 第二次运行时,现有的
Xsolution
不适合新X的大小。
this is another reason why you always want to allocate the array before using it. 这是为什么您总是要在使用之前分配数组的另一个原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.