Simple Way to Find How Far Down a Number Is

조회 수: 3 (최근 30일)
Jon
Jon 2022년 11월 2일
댓글: Star Strider 2022년 11월 2일
Hello,
I was wondering if there is a simple way to determine which row a number appears?
For example, if I have a large table with the last column is either a 1 or 0, if I want to know which row holds a 0, how can I make an array that essentially says: The value 0 appears in rows 1, 34, 89, ...
I found this forum post, which goes over how to pull a number from a column of the same row based on the value in another column.
I was thinking I could make a new column that just numbers from 1 to N (where N is the number of rows) and add that as a new column to the table.
Then using the technique from the linked forum post, save the number (value) from the added column into an empty matrix (an all-zero array from 1 to N).
Then after that, remove all the zeros and be left with an array that only has the postitions of where the 0 is in the original column.
This method seems long but it can work, I was just wondering if there is something simpler to do.
Thanks.

채택된 답변

Star Strider
Star Strider 2022년 11월 2일
Use the find function —
T1 = array2table([rand(10,3) randi([0 1], 10, 1)])
T1 = 10×4 table
Var1 Var2 Var3 Var4 _______ _______ _______ ____ 0.63964 0.82422 0.12981 0 0.591 0.70306 0.62215 0 0.51874 0.22951 0.43027 0 0.42188 0.80246 0.65887 1 0.14287 0.80063 0.2118 0 0.73422 0.92003 0.11586 1 0.67137 0.43841 0.50498 1 0.52176 0.91758 0.3801 0 0.12744 0.61668 0.65305 0 0.45125 0.19285 0.96927 0
RowIdx = find(T1.Var4 == 0)
RowIdx = 7×1
1 2 3 5 8 9 10
.
.
  댓글 수: 2
Jon
Jon 2022년 11월 2일
Sweet, thank you very much.
That is much simpler than what I though about doing.
Star Strider
Star Strider 2022년 11월 2일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by