Verify a matrix value on MATLAB

前端 未结 3 1127
醉话见心
醉话见心 2021-01-22 05:41

I am using MATLAB. I have a question about how i can verify that the values of a matrix are being repeating, like this:

A=[ 2 3 2 3 2 3 2 3]

If

3条回答
  •  不知归路
    2021-01-22 06:17

    The following one-liner works if A is a row or column vector, but not necessarily if it is a matrix (thanks to @Dan for providing a simplification in comments). I figured this would be okay since the example you provide in the question is a vector.

    AUX = ~any(A(3:end) - A(1:end-2))
    

    This vectorized solution should be a lot faster than the non-vectorized solution supplied by @Nirk (for large A).

    Depending on your application you may need to include the error trap:

    if size(A, 2) < 3; error('Input matrix needs to have at least 3 columns'); end
    

    Note, see the comments on this answer for some alternative ways of dealing with the case size(A, 2) < 3.

提交回复
热议问题