4,193 views (last 30 days)
Show older comments
Mykhailo Yaroshenko on 8 Nov 2017
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array
Edited: MathWorks Support Team on 5 Jun 2024 at 13:16
Accepted Answer: 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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
James Tursa on 15 May 2024 at 0:00
Edited: MathWorks Support Team on 5 Jun 2024 at 13:16
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 Comments Show 5 older commentsHide 5 older comments
Show 5 older commentsHide 5 older comments
Stephen23 on 9 Nov 2017
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_502775
Open in MATLAB Online
Often logical indexing is more efficient, so you might only need this:
idx = X==5;
Scott MacKenzie on 18 Apr 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1467076
Edited: Scott MacKenzie on 18 Apr 2021
Open in 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 on 11 Sep 2021
Direct link to this comment
https://www.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 on 11 Sep 2021
Direct link to this comment
https://www.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 on 11 Sep 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_1731264
Open in 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 on 2 Oct 2021
Direct link to this comment
https://www.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 on 29 Jun 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/365917-how-can-i-find-index-of-element-in-array#comment_2799898
Open in 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]}
Sign in to comment.
More Answers (0)
Sign in to answer this question.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office