How to keep rows whose values follow a certain sequence?
古いコメントを表示
I have a matrix composed of two columns A and B, I need to only keep the rows which contain the sequence 90-180-270:
A B A B
-------- ----------
90 1 90 1
180 9 180 9
270 2 270 2
90 0 -> 90 0
270 3 180 4
90 0 270 6
180 4
270 6
How can I implement this into a Matlab code?
5 件のコメント
Stephen23
2022 年 2 月 3 日
What are the possible values in column A ? Can it also include 4, 5, 9, 123, or does it include only 1, 2, & 3 ?
Lu Da Silva
2022 年 2 月 3 日
編集済み: Lu Da Silva
2022 年 2 月 3 日
David Hill
2022 年 2 月 3 日
Do you want 90,180,270 sequence?
"It only includes three possible numbers: 180, 270 and 90. (I wrote 1 2 3 for simplicity reasons)."
It is more complex and slower. Because solutions that work with 1-2-3 might not work with other values. But because you gave us incorrect information we all spend our time developing solutions that might not suit your needs at all.
You should tell us the exact values and the exact order.
Lu Da Silva
2022 年 2 月 3 日
編集済み: Lu Da Silva
2022 年 2 月 3 日
採用された回答
その他の回答 (1 件)
David Hill
2022 年 2 月 3 日
s=strfind(num2str(yourMatrix(:,1))','123');
newMatrix=[];
for k=1:length(s)
newMatrix=[newMatrix;yourMatrix(s(k):s(k)+2,:)];
end
2 件のコメント
Lu Da Silva
2022 年 2 月 3 日
David Hill
2022 年 2 月 3 日
This should work.
r=yourMatrix(:,1);
r(r==90)=1;r(r==180)=2;r(r==270)=3;%change based on sequence desired
f=strfind(num2str(r)','123');
newMatrix=[];
for k=1:length(f)
newMatrix=[newMatrix;yourMatrix(f(k):f(k)+2,:)];
end
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!