初めて0が現れる行数を特定するには

27 visualizaciones (últimos 30 días)
yu tabata
yu tabata el 10 de Nov. de 2018
Comentada: Yoshio el 12 de Nov. de 2018
0と1が並ぶ行列(500x1)があります。 行列中に初めて0が現れる行数を特定する、行数をスカラーとして返す方法はありますか?
  1 comentario
Yoshio
Yoshio el 12 de Nov. de 2018
forや whileを使う基本的なアルゴリズムは大丈夫ですか?

Iniciar sesión para comentar.

Respuestas (2)

Kei Otsuka
Kei Otsuka el 10 de Nov. de 2018
find関数を利用する方法は如何でしょうか。
% 適当に行列作成
data = round(rand(500, 1));
 
% 論理反転した後、find関数で非ゼロ要素を検索
idx = find(~data);
 
% 最小値(=初めて0が出現した行数)を求める
min(idx)
  1 comentario
Yoshio
Yoshio el 12 de Nov. de 2018
[~,ind] = min(data) でも行けそうです。0がたまたま最小値なのと、minの仕様で。
"最小値が複数回発生する場合、min は、最初に発生した最小値に対応するインデックスを返します。"

Iniciar sesión para comentar.


mizu
mizu el 11 de Nov. de 2018
直接の回答ではありませんが、無料のオンラインコースをやると、基礎的な操作がわかるかと https://matlabacademy.mathworks.com/jp

Categorías

Más información sobre 論理演算 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!