matlab - Attempted to access b(1,2); index out of bounds because size(b)=[3,1] -
i have uni assignment create basic function works same backslash command in matlab. i'm not looking answer uni, intentions not cheat, i'm stuck on problem , need guidance on how work.
so think i'm close answer.
function x = matrix_solve(a,b) inva=inv(a); [m,n] = size(a); % m num rows, n num of columns nb = size(b,1); if m == n && nb == m x = zeros(m,n); = 1:m j = 1:n x(1,j) = x(1,j)+ inva(i,1)*b(1,j); end end else error('error') end
using basic square matrix
b = [1;3;2] = [3,1,2;3,2,1;3,3,3]
i keep getting error mentioned in title.
i'm pretty familiar other code i'm very new matlab.
the cause of index out of bound error you're trying access element doesn't exist @ line
x(1,j) = x(1,j)+ inva(i,1)*b(1,j);
instead should
x(1,j) = x(1,j)+ inva(i,1)*b(j,1);
i.e. change b(1,j)
b(j,1)
. b
is column vector of size 3 (size(b)=[3,1]
) , trying access element @ row 1, column 2, there 1 column.
Comments
Post a Comment