MATLAB Answers

how to use an if statement to determine whether an index is out of bounds

22 views (last 30 days)
kals;djf;alkdjf on 20 Sep 2020
Commented: Turlough Hughes on 22 Sep 2020
hi everyone! Thank you so much for attempting to help me!
The question may be a little unclear. Let me explain.
I have two matrixes, both are a 3x3. I also have 1 other matrix--a 1x2 and called 'indices'. The 1x2 matrix will represent the the index of both 3x3 matrixes. For example, [2 2 ] means I want to find the number located in postion 2,2 on both matrixes. [3 3], number in position 3,3 on both matrixes, etc
Now, I want to use an if statement to ensure the 1x2 matrix is within bounds of the 3x3 matrix. I don't want the 1x2 matrix, for example, to be [4 4] because I can't find the number in position 4,4 on both 3x3 matrixes. I also don't want the 1x2 matrix to be [0 0] because matlab begins indexing at 1. I know that negative indexing exists in matlab, but my class has not hit on this topic yet. Thus, don't worry about negative indexing. The 1x2 matrix will never contain any negative numbers. If indices is outside of bounds, I want to display ''Error: index is out of bounds.'
So far I have:
But I am not getting it right. The computer spits out it's own error, not my personalized ''Error: index is out of bounds.'


the cyclist
the cyclist on 20 Sep 2020
I don't see any inherent problem with the one line of code you have posted. (In the future, please post the actual code, rather than an image of your code.)
Without more context, I don't see the problem. Can you post the entire code (or a distilled example that give the error), and/or the full error message you are getting from MATLAB.
You can attach files using the paper clip icon in the INSERT section of the toolbar.
Sindar on 21 Sep 2020
Is this if statement meant to check whether the indices are valid or invalid? It looks fine for finding valid inputs (assuming indices are created in such a way that they'll be integers), but you describe the goal as finding invalid inputs
Also, Matlab does not have negative indexing (that's Python). Instead, Matlab uses the 'end' keyword

Sign in to comment.

Answers (1)

Turlough Hughes
Turlough Hughes on 21 Sep 2020
If indices is outside of bounds, I want to display ''Error: index is out of bounds.'
The conditional expression in your if block is checking if indices are inside the bounds.To throw an error when indices are outside of bounds you could write the following:
if any(indices < 1 | indices > 3)
error('Error: index is out of bounds.')
using short-circuit logical expressions this can also be written as:
if indices(1)<1 || indices(2)<1 || indices(1)>3 || indices(2)>3
error('Error: index is out of bounds.')
if any(indices)<1 || any(indices)>3
I should also add, a better approach than using an if block with an error statement is to use the assert function (see documentation). In this case you assert that an expression is true, otherwise, throw an error. For example:
assert(all(indices>=1 & indices<=3),'Error: index is out of bounds.')


Sindar on 21 Sep 2020
Out of curiosity, what's better about assert? I considered switching over but found the if block formulation easier to read (esp. to notice while scrolling in a long code) and more flexible ("eh, it'll be fine, let's warn instead")
Turlough Hughes
Turlough Hughes on 22 Sep 2020
Assert is more concise and direct. When I write:
assert(expression,'error statement')
it's meaning is simply "assert that this condition is true". The assertion communicates directly to the reader what the programmer intended, and instead of thinking what ought not be true (and often times not not be true) you are thinking about what ought to be true for the code to work as expected. Also, if one part of your code stands out more the result is other parts stand out less so that doesn’t improve readability.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by