配列内の数値の位置を見つけるにはどうしたらよいですか?
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 14 Nov 2024
Réponse apportée : MathWorks Support Team
le 14 Nov 2024
ベクトル a = [7 8 8 2 5 6] がある場合、値8の位置をどのように計算すればよいですか?期待する結果は、2と3、または(1,2)と(1,3)です。
Réponse acceptée
MathWorks Support Team
le 14 Nov 2024
配列の要素の値に対応する位置を返すには、find関数を使用することができます。例えば:
a = [7 8 8 2 5 6];
linearIndices = find(a==8)
この場合、linearIndicesには以下の値が格納されます:
2 3
行と列のインデックスを個別に取得するには、次のようにします:
[row,col] = find(a==8)
この結果、rowとcolには以下の値が格納されます:
row =
1 1
col =
2 3
もし、一度だけ出現する位置が必要な場合は、find(a==8,1)という構文を使用できます。また、最初または最後の出現位置を特定したい場合は、find(a==8,1,'first')のように方向を指定することも可能です。これらのオプションについての詳細は、findのドキュメントをご参照ください。
0 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!