ERROR: Index exceeds matrix dimensions

4 visualizaciones (últimos 30 días)
Jason Abraham
Jason Abraham el 31 de Oct. de 2020
Comentada: Jason Abraham el 1 de Nov. de 2020
Quite new to matlab, I was doing some practice questions but when I came across this particular one I hit a brick wall.
%---------------------------------------------------------------------------------------------------------------------------
% Basic rectangular domain with uniform separation h between nodes
clc;clear all;close all;
hx = 0.01;
hy = 0.005;
Nx = 1/hx+1 ;
Ny = 4/hy+1 ;
x = linspace(0,1,Nx) ;
y = linspace(0,4,Ny) ;
x = x' ;
y = y' ;
%Create a mapping from the 2-D lattice indices to a single vector index by
%labelling all the points in the domain
VecToCoord = zeros(0,2) ; %First index is the vector inex, first column gives the xindex and the second gives the yindex
for i = 1:Nx
VecToCoord = [VecToCoord;[[1:Nx]',i*ones(Nx,1)]];
end
CoordToVec = zeros(Nx,Ny); %first element is the xindex and second is the yindex
count = 1 ;
for j = 1:Ny
for i = 1:Nx
CoordToVec(i,j) = count ;
count = count + 1 ;
end
end
N= Nx*Ny; %Total number of nodes in whole domain
%Store Laplacian stencil
stencil = [1,1,-4,1,1] ;
%Find matrix for all nodes
A = sparse(Nx,Ny) ;
%RHS vector
b = zeros(Nx,1) ;
f = @(x,y) 50*sin(4*pi*x*(1-x))-50*cos(4*pi*y);
for i =1:N
xCoord = VecToCoord(i,1) ; %<----------------------ERROR:INDEX EXCEEDS MATRIX DIMENSIONS
yCoord = VecToCoord(i,2) ;
xval = x(VecToCoord(i,1)) ;
yval = y(VecToCoord(i,2)) ;
%-----------------------------------------------------------------------------------------------------------------------
if x(xCoord) == 0 || x(xCoord) == 1 || y(yCoord) == 0 || y(yCoord) ==4 %BOUNDARIES
%Boundary node
A(i,i) = 1 ; %implement dirichlet condition
b(i) = yval-xval ;
else %Internal nodes
upInd = i + Nx;
downInd = i - Nx;
leftInd = i - 1 ;
rightInd = i + 1 ;
%elements of update matrix in row i
A(i,upInd) = 1/(hy^2) ;
A(i,downInd) = 1/(hy^2) ;
A(i,i) = -2/hx^2 -2/hy^2 ;
A(i,leftInd) = 1/(hx^2) ;
A(i,rightInd) = 1/(hx^2) ;
b(i) = f(xval,yval) ;
end
end
sol = A\b ;
%Convert to a matrix to plot as surface
sol_Matr = zeros(Nx,Ny) ;
for i = 1 : Nx
for j = 1 :Ny
sol_Matr(i,j) = sol(CoordToVec(i,j)) ;
end
end
surf(x,y,sol_Matr')
hold on
shading interp
xindex = find(x==0.5) ;
yindex = find(y==0.5) ;
sol_Matr(xindex,yindex)
I'm pretty sure the cause of the error lies between the two long dashed lines(--------) that I put in the codes. I've also pointed out which line is the error in the codes. I am aware that the error is occuring due to me trying to access an element past the actual length of the array and thus it wouldn't exist, but as most of it is a skeleton code where I just had to tweak about here and there, I am not sure whats causing the problem. I tried changing some stuff about to no avail. I would highly appreciate it if someone could point out where the problem is and advice me on how to change those lines. Thank you very much :)

Respuestas (1)

Image Analyst
Image Analyst el 31 de Oct. de 2020
It's because the matrix has 10201 rows and you're trying to go up to N which is 80901. So you can only go as many rows as you have, which is size(VecToCoord, 1):
for i =1: size(VecToCoord, 1)
xCoord = VecToCoord(i,1) ; %<----------------------ERROR:INDEX EXCEEDS MATRIX DIMENSIONS
  3 comentarios
Image Analyst
Image Analyst el 1 de Nov. de 2020
Well just hover over the variables to see what their values and sizes are.
Like what is the size of CoordToVec, i, and j? Obviously either i or j is bigger than the corresponding dimension of CoordToVec.
Jason Abraham
Jason Abraham el 1 de Nov. de 2020
Yea the i is definitely the problem. I have to keep the for loop to run from 1 to N. so I realised to do that I have to increase the matrix size to N x N, so in my case to 80901 x 80901 Sparse double. And I also realised that my i should equal my Nx and j shold equal Ny, but for some reason the i jumps to 10202 instead of the 101 it should be. I ran the codes section by section and realised i stays at 101 until the FIRST for loop begins, after the for loop it jumps to the 10202. Anyway for me to fix that?

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by