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

62 ビュー (過去 30 日間)
kals;djf;alkdjf
kals;djf;alkdjf 2020 年 9 月 20 日
コメント済み: Turlough Hughes 2020 年 9 月 22 日
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.'
  2 件のコメント
the cyclist
the cyclist 2020 年 9 月 20 日
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
Sindar 2020 年 9 月 21 日
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

サインインしてコメントする。

回答 (1 件)

Turlough Hughes
Turlough Hughes 2020 年 9 月 21 日
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.')
end
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.')
end
%or
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.')
  2 件のコメント
Sindar
Sindar 2020 年 9 月 21 日
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 2020 年 9 月 22 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by