Matlab Out of Bounds Error Solved

In summary, the conversation was about an error that occurred while running a code. The error was caused by trying to access an index that did not exist, specifically p(2,11,1). This was most likely due to a mistake in the code, where the variable j was used instead of i in the second for loop. This was quickly resolved and the issue was fixed.
  • #1
MaxManus
277
1
Hey, I have defined
Code:
pu = zeros(nx,ny,N);
pv = zeros(nx,ny,N);
pu(:,ny,:) = 1;

and written the loop:
line 38-40
Code:
 for i = 2:(nx-1);
        for i = 2:(ny-1);
            ps(i,j) = p(i,j,n) - a1*(pu(i+1,j,n) - pu(i,j,n)) -a2*(pv(i,j+1,n) - pv(i,j,n));

and I get the error
? Attempted to access pv(2,11,1); index out of bounds because size(pv)=[10,10,10].

Error in ==> fluidmekanikk at 40
ps(i,j) = p(i,j,n) - a1*(pu(i+1,j,n) - pu(i,j,n)) -a2*(pv(i,j+1,n) - pv(i,j,n));

Could someone help me with the error?
 
Physics news on Phys.org
  • #2
Well, obviously, you tried to access p(2,11,1) which does not exist.

Looking at the indexes, it was probably pv(i,j+1,n), since that goes highest int he middle term.

Now... what range does j iterate over?
 
  • #3
Hurkyl said:
Well, obviously, you tried to access p(2,11,1) which does not exist.

Looking at the indexes, it was probably pv(i,j+1,n), since that goes highest int he middle term.

Now... what range does j iterate over?

Thanks for fast reply and solution.
The code
Code:
for i = 2:(nx-1);
        for i = 2:(ny-1);
was supposed to be
Code:
for i = 2:(nx-1);
        for j = 2:(ny-1);
and I had used the j as variable earlier.
 
Last edited:

What is the "Matlab Out of Bounds Error"?

The "Matlab Out of Bounds Error" is an error message that occurs when you try to access an element of an array or matrix that is outside of its defined range.

Why does the "Matlab Out of Bounds Error" occur?

The "Matlab Out of Bounds Error" occurs when you attempt to access an element of an array or matrix with an index that is greater than the size of the array or matrix, or less than 1.

How can I solve the "Matlab Out of Bounds Error"?

To solve the "Matlab Out of Bounds Error", you need to make sure that the index you are using to access elements of an array or matrix is within its defined range. You can also use the "try-catch" statement to catch the error and handle it in your code.

Can I prevent the "Matlab Out of Bounds Error" from occurring?

Yes, you can prevent the "Matlab Out of Bounds Error" from occurring by checking the size of your array or matrix and the index you are using before trying to access an element. You can also use the "isempty" function to check if the array or matrix is empty before attempting to access its elements.

Are there any other common causes of the "Matlab Out of Bounds Error"?

Other common causes of the "Matlab Out of Bounds Error" include using incorrect indices when accessing elements of an array or matrix, and trying to access elements of a multidimensional array using a single index instead of multiple indices for each dimension.

Similar threads

Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
500
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Back
Top