行列から条件を指定して値を取り出す

以下のような行列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
どのようにすればよいか教えていただけると幸いです。

 Respuesta aceptada

Akira Agata
Akira Agata el 12 de Nov. de 2020

0 votos

以下のような方法はいかがでしょうか?
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

1 comentario

Masa
Masa el 12 de Nov. de 2020
ありがとうございます!
助かりました参考にさせていただきます。

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 12 de Nov. de 2020

Comentada:

el 12 de Nov. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!