行列から条件を指定して値を取り出す
4 views (last 30 days)
Show older comments
以下のような行列Aの2行目の要素が0から1に変わった直後の1行目の値を抽出して別の行列として定義したいのですがうまく表現できません.。
次のような行列Aがあるとします.
A=
1 2 3 4 5 6 7
0 1 1 0 1 1 0
このとき,2行目の要素が2行目の要素が0から1に変わった直後の1行目の値を抜き出し,以下のような行列Bとしたいです.
B=
2 5
どのようにすればよいか教えていただけると幸いです。
0 Comments
Accepted Answer
Akira Agata
on 12 Nov 2020
以下のような方法はいかがでしょうか?
A = [1 2 3 4 5 6 7; 0 1 1 0 1 1 0];
idx = [0 diff(A(2,:))] == 1;
B = A(1,idx);
結果:
>> B
B =
2 5
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!