简体   繁体   English

在Matlab中查找矩阵中相同元素的起始索引和结束索引

[英]Find the Start Index and End Index of the same Element in a Matrix in Matlab

I want to get the start index and end Index of the same repetitive Element in a matrix?我想获取矩阵中相同重复元素的开始索引和结束索引? for example this Matrix:例如这个矩阵:

矩阵

the result will be:结果将是:

结果

zero is not considered不考虑零

You can go this way (example for 1):您可以这样做(例如 1):

a = rem(find(A==1),5)
a(a==0)=5;
startidx = min(a)
endidx = max(a)

The same way change A==1 to whatever you need and you'll get the result.以同样的方式将A==1更改为您需要的任何内容,您将得到结果。 You also can create a function with parameters A, number_you_want_to_find .您还可以创建一个带有参数A, number_you_want_to_find的函数。 There are some ways to improve this code, for example instead 5 use size(A,1) and maybe there is some way to replace all this code with one line, but this works too!有一些方法可以改进此代码,例如使用size(A,1)代替5 ,也许有一些方法可以用一行替换所有这些代码,但这也有效!

Hope, it was helpful!希望,很有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM