Borrar filtros
Borrar filtros

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

454 visualizaciones (últimos 30 días)
Kouki
Kouki el 28 de Oct. de 2019
Comentada: Kouki el 28 de Oct. de 2019
以下のような行列Aの1列目の要素が"3"である行を抽出して別の行列として定義したいのですがうまく表現できません.
次のような行列Aがあるとします.
A=
1 -66
2 -61
3 -65
3 -64
1 -66
このとき,1列目の要素が"3"である行を抜き出し,以下のような行列Bとしたいです.
B=
3 -65
3 -64
----------------------------------------------------------------------------------------------------------------------
%Aの1行目が3である行を抽出しBとする
A=[1 -66 ; 2 -61 ; 3 -65 ; 3 -64 ; 1 -66];
B=A[A[:,1].==3,:]

Respuesta aceptada

Takumi
Takumi el 28 de Oct. de 2019
Editada: Takumi el 28 de Oct. de 2019
find関数を使うといいと思います.非ゼロ要素のインデックスと値を見つける
A=[1 -66 ; 2 -61 ; 3 -65 ; 3 -64 ; 1 -66];
row = find(A(:,1)==3); % Aの1列目が3である行を抽出
B = A(row,:)
  3 comentarios
michio
michio el 28 de Oct. de 2019
参考まで:条件を満たす行数を明示的に求める必要が無い場合は、find を使うステップを省くことも可能です。
A=[1 -66 ; 2 -61 ; 3 -65 ; 3 -64 ; 1 -66];
B = A(A(:,1)==3,:)% Aの1列目が3である行を抽出
Kouki
Kouki el 28 de Oct. de 2019
回答ありがとうございます.
簡潔でわかりやすいのでこちらも参考にさせていただきます.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!