samiam2013
samiam2013

Reputation: 3

I need a function that will return the existence (or not) of a given index of a matrix

I'm trying to filter noise out of an image in matlab and I've hit an issue. I need to be able ask some function whether or not if the index -1,-3 or -1,4 or 5,-1 exists, and I need it to return some integer or a false so that I can put it in an if statement. so far, with islogical() exist() and just ARRAYNAME(-1,4) I've gotten an error saying that index position doesn't exist (duh, but that's not what I want) is there a function that can return 1 if there's an error? I really just need this one thing. let me know if the question is too vague.

Upvotes: 0

Views: 43

Answers (1)

Autonomous
Autonomous

Reputation: 9075

You can use try-catch statement as follows.

function element=neverReturnIndexingError(array1)
%array1=[1 2 3 4];
try
    element=array1(-1,2);
catch
    fprintf('Index is invalid\n');
    element=1; %returning 1 as you said
end

Upvotes: 2

Related Questions