How can I find index of element in array? (2024)

조회 수: 4,462 (최근 30일)

이전 댓글 표시

Mykhailo Yaroshenko 2017년 11월 8일

  • 링크

    이 질문에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array

  • 링크

    이 질문에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array

편집: MathWorks Support Team 대략 6시간 전

채택된 답변: James Tursa

I know that I have a number 5 as an element in array X, but I do not know its index. Does MATLAB have a built-in function similar to Python's "index" method for finding the index of an element in an array?

댓글 수: 0

이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

이 질문에 답변하려면 로그인하십시오.

채택된 답변

James Tursa 2024년 5월 15일 0:00

  • 링크

    이 답변에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#answer_290079

  • 링크

    이 답변에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#answer_290079

편집: MathWorks Support Team 대략 4시간 전

To find the index of a specific integer value (without roundoff error) in an array of integers, use the "find" functionand == operator. For example, find the index of an element equal to 5 in a 1-by-11 vector of integers.

x = 0:1:10;

k = find(x==5)

To find a numeric value in an array of floating-point numbers, use a tolerance value based on your data. Otherwise, the result is sometimes an empty matrix due to floating-point roundoff errors. For example, find the index of an element equal to 0.5 within a roundoff error of 1e-6.

y = 0:0.1:1;

k = find(abs(y-0.5) < 1e-6)

댓글 수: 7

이전 댓글 5개 표시이전 댓글 5개 숨기기

Stephen23 2017년 11월 9일

이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_502775

  • 링크

    이 댓글에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_502775

MATLAB Online에서 열기

Often logical indexing is more efficient, so you might only need this:

idx = X==5;

Scott MacKenzie 2021년 4월 18일

이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1467076

  • 링크

    이 댓글에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1467076

편집: Scott MacKenzie 2021년 4월 18일

MATLAB Online에서 열기

Bear in mind that if the number occurs more than once in the vector, the result returned is a vector containing the indices of all occurrences. If you want the index of just the first occurrence of the number, insert 1 as the second argument in find:

>> x = [3 4 5 6 4 8]

x =

3 4 5 6 4 8

>> result = find(x==4)

result =

2 5

>> result = find(x==4, 1)

result =

2

Walter Roberson 2021년 9월 11일

이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1731234

  • 링크

    이 댓글에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1731234

Henrik Wassertheurer comments to James Tursa

Doesn't answer the question

Image Analyst 2021년 9월 11일

이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1731259

  • 링크

    이 댓글에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1731259

Henrik, yes it does, as I understand the question. Why do you say it doesn't?

Walter Roberson 2021년 9월 11일

이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1731264

  • 링크

    이 댓글에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1731264

MATLAB Online에서 열기

@Henrik Wassertheurer

What difficulty do you find with James' answer? He showed the find() function, which is the function defined to locate the places where a condition occurs.

If you need to have the exact question answered more clearly, "but did Matlab doesn't have build-in similar function?" then the answer to that is "NO, MATLAB does not have a built-in function in which you can provide only the array name and the value, and MATLAB will return all the indices of the value in the array."

Note: if you only need to know the first location, then you can also use

[~, result] = ismember(5, x)

result will be 0 if 5 is not present in x.

Ehsan Partovi 2021년 10월 2일

이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1764809

  • 링크

    이 댓글에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1764809

The function find() is useful as far as matrices (2-D tensors) are concerned. I cannot, however, find a useful function for nd-arrays where, for instance, the index could be an array on its own. See example below:

M = reshape(1:24, [2,3,4]);

indices = index_finder(M==20); % indices = vector of indices

It would be very useful if there was a function which worked for tensors of any dimensionality.

Jesse Ivers 2023년 6월 29일

이 댓글에 대한 바로 가기 링크

https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_2799898

  • 링크

    이 댓글에 대한 바로 가기 링크

    https://kr.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_2799898

MATLAB Online에서 열기

@Ehsan Partovi I couldn't agree with you more; this is a problem I seem to run into often, and here is my solution:

% Example ND-array

arr = reshape([1:6000], [5 5 10 4 6]);

numberOfInterest = 99;

% Get the linear index of the

linearIndex = find(arr==numberOfInterest);

% Convert linear index to subscript

[row, col, depth, channel, time] = ind2sub(size(arr), linearIndex)

row = 4

col = 5

depth = 4

channel = 1

time = 1

The only drawbacks are the reuirement that you know how many dimensions. YOu can get around this with CSLs like so:

% Use CSL to get all the outputs

[idicies{1:ndims(arr)}] = ind2sub(size(arr), linearIndex)

idicies = 1×5 cell array

{[4]} {[5]} {[4]} {[1]} {[1]}

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

추가 답변 (0개)

이 질문에 답변하려면 로그인하십시오.

참고 항목

카테고리

MATLABLanguage FundamentalsMatrices and Arrays

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

  • find

제품

  • MATLAB

릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

오류 발생

페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.


Translated by How can I find index of element in array? (10)

How can I find index of element in array? (11)

웹사이트 선택

번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:

또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.

미주

  • América Latina (Español)
  • Canada (English)
  • United States (English)

유럽

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

아시아 태평양

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

지역별 지사에 문의

How can I find index of element in array? (2024)
Top Articles
Craigslist Of Odessa Texas
The ultimate guide to Norwegian Cruise Line ships and itineraries - The Points Guy
Toa Guide Osrs
Kem Minnick Playboy
Washu Parking
Ffxiv Palm Chippings
Breaded Mushrooms
T Mobile Rival Crossword Clue
Apex Rank Leaderboard
Free VIN Decoder Online | Decode any VIN
Tap Tap Run Coupon Codes
Noaa Swell Forecast
Draconic Treatise On Mining
Employeeres Ual
Campaign Homecoming Queen Posters
World Cup Soccer Wiki
R Tiktoksweets
George The Animal Steele Gif
Les Rainwater Auto Sales
Craigslist Free Stuff Santa Cruz
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
2024 INFINITI Q50 Specs, Trims, Dimensions & Prices
Pjs Obits
All Breed Database
‘The Boogeyman’ Review: A Minor But Effectively Nerve-Jangling Stephen King Adaptation
F45 Training O'fallon Il Photos
Prep Spotlight Tv Mn
Kabob-House-Spokane Photos
Cardaras Funeral Homes
EVO Entertainment | Cinema. Bowling. Games.
Claio Rotisserie Menu
Bfsfcu Truecar
Where to eat: the 50 best restaurants in Freiburg im Breisgau
Dl.high Stakes Sweeps Download
Word Trip Level 359
Solemn Behavior Antonym
Gvod 6014
2020 Can-Am DS 90 X Vs 2020 Honda TRX90X: By the Numbers
SF bay area cars & trucks "chevrolet 50" - craigslist
Puretalkusa.com/Amac
Engr 2300 Osu
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Ladyva Is She Married
Free Crossword Puzzles | BestCrosswords.com
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Best Restaurant In Glendale Az
Great Clips Virginia Center Commons
Roller Znen ZN50QT-E
View From My Seat Madison Square Garden
Grace Charis Shagmag
Hy-Vee, Inc. hiring Market Grille Express Assistant Department Manager in New Hope, MN | LinkedIn
Epower Raley's
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6251

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.