問題描述
在Matlab中找到矩陣中相同元素的開始索引和結束索引 (Find the Start Index and End Index of the same Element in a Matrix in Matlab)
我想獲取矩陣中相同重複元素的開始索引和結束索引?例如這個矩陣:
結果將是:
不考慮零
參考解法
方法 1:
You can go this way (example for 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. You also can create a function with parameters 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!
Hope, it was helpful!
(by Zigouma、Mikhail_Sam)