MATLAB: How can i input a value and then determine it if this value is similar to one of the matrix

#question #important #easy

Hi, I need your help please; I have a matrix of 2-by-2, I named it A; I need to ask the user to input any number; I want to know if there is a function can check the input number and see if it's the same as one of my created matrix values. Here is the script;
N=input('Enter Any Number : ')
Result= %Here, I want Matlab to check the input if it's one of the A matrix. I want the result telling me if yes (1) then it will let the user complete. But if the result is no (0), it will stop the user to not complete.

Best Answer

  • You should use the 's' option with input:
    A = [1,2;3,4];
    N = str2double(input('Enter Any Number : ','s'));
    if any(N==A(:))
    disp('No match :(')
    If you want to check "similarity", as your title states, then use a tolerance:
    tol = 0.5;
    if any(abs(N-A(:))<tol)