行ベクトルから不連続​な要素を3つ抽出する​にはどうすれば良いか

118 visualizaciones (últimos 30 días)
Yudai Watanabe
Yudai Watanabe el 5 de Feb. de 2021
Comentada: Yudai Watanabe el 5 de Feb. de 2021
MATLABのチュートリアルをしていたところ追加の練習に列ベクトルから不連続な要素を抜き取るというものがありましたがやり方がわかりませんでした。 例えばa=[1;2;3;4;5;6;7]から一度に1,3,6を抽出するにはどのようにすれば良いのでしょうか。

Respuesta aceptada

Shunichi Kusano
Shunichi Kusano el 5 de Feb. de 2021
こんにちは。
まんまになってしまいますが、取り出したいインデックスをベクトル化すればいいです。
a=[1;2;3;4;5;6;7]; % 対象ベクトル
idx = [1 3 6];
a(idx) % 1 3 6が取り出せる
蛇足かもしれませんが、条件を何か指定してやって当てはまるインデックスだけを取り出すにはfind関数とかが使えます。
idx = find(mod(a,2) == 1); % 奇数であればtrueとなり、あてはまるインデックスが取得できる
a(idx) % 1 3 5 7
ご参考まで。
  1 comentario
Yudai Watanabe
Yudai Watanabe el 5 de Feb. de 2021
御回答有難う御座います。 実際に試したところ意図通りとなりました。 有難う御座いました。

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!