在MATLAB上验证矩阵值 [英] Verify a matrix value on MATLAB

查看:129
本文介绍了在MATLAB上验证矩阵值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MATLAB.我有一个关于如何验证矩阵值是否正在重复的问题,例如:

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]

如果矩阵A对于之后的所有列至少重复前两个值,则我希望AUX =1.但是,如果不是,则仅AUX = 0.

If the matrix A repeat at least the first two values for all columns after, i want a AUX = 1. but if not, only AUX= 0.

推荐答案

如果A是行或列向量,但不一定是矩阵(如果使用@Dan可以提供一个简化注释).我认为这可以,因为您在问题中提供的示例是向量.

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))

此矢量化解决方案应该比@Nirk提供的非矢量化解决方案快很多(对于大型A).

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

请注意,有关处理案件size(A, 2) < 3的其他替代方法,请参见此答案的评论.

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

这篇关于在MATLAB上验证矩阵值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆