行列から条件を指定して値を取り出す
148 views (last 30 days)
Show older comments
以下のような行列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,:]
0 Comments
Accepted Answer
Takumi
on 28 Oct 2019
Edited: Takumi
on 28 Oct 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 Comments
michio
on 28 Oct 2019
参考まで:条件を満たす行数を明示的に求める必要が無い場合は、find を使うステップを省くことも可能です。
A=[1 -66 ; 2 -61 ; 3 -65 ; 3 -64 ; 1 -66];
B = A(A(:,1)==3,:)% Aの1列目が3である行を抽出
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!