find first& end of array
5 ビュー (過去 30 日間)
古いコメントを表示
採用された回答
Stephen23
2022 年 5 月 20 日
S = [0,1,5,2,0,0,0,9,3,50,53,0,0,5,7,4]
X = diff([0;S(:)]==0)<0 | diff([S(:);0]==0)>0;
V = S(X)
0 件のコメント
その他の回答 (2 件)
the cyclist
2022 年 5 月 20 日
I expect someone will post a more elegant method, but I think this does what you want
S=[0 1 5 2 0 0 0 9 3 50 53 0 0 5 7 4];
first = S(diff([0 S]) == S & S~=0);
last = S(diff([S 0]) ==-S & S~=0);
firstLast = [first; last];
output = firstLast(:)'
0 件のコメント
Image Analyst
2022 年 5 月 20 日
Here's a different way:
S=[0 1 5 2 0 0 0 9 3 50 53 0 0 5 7 4];
S2 = [0,S,0];
indexes = sort([strfind(S2~=0, [0 1]) + 1, strfind(S2~=0, [1,0])])
S3 = S2(indexes)
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!