How can I get the indxes from a logical vector?

조회 수: 3 (최근 30일)
Ali Almakhmari
Ali Almakhmari 2023년 9월 11일
답변: Dyuman Joshi 2023년 9월 11일
Hi guys, I feel like my question is silly and has a straightforward answer, but I am ashamed to say that I have been at it for the past hour or so. I hope someone can help.
I have a column vector that approximately has 1000 rows (1000 by 1). All logical values: 1 or 0. The elements that have 1, I would like to save their index, and the elements that have zero, I would like to ignore their index. For example, lets say I have an input vector that is input = [1,1,1,0,0,1,0,1], then my output will be output=[1,2,3,6,8]. I saved the index of all places where there was a 1 in the input. Which is what I want to obtain from any similar input vector.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 9월 11일
find is what you are looking for.
input = [1,1,1,0,0,1,0,1];
output = find(input)
output = 1×5
1 2 3 6 8

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by