How to find the position of a number in an array?
Show older comments
If I have a vector, a = [7 8 8 2 5 6], how do I compute the positions of the value 8? I expect 2 and 3 or (1,2) and (1,3).
Accepted Answer
More Answers (4)
Bhagyesh Shiyani
on 5 Dec 2019
3 votes
what if i want both 8 positions, any code?
2 Comments
Florian Reinbold
on 15 Jan 2020
Hi Bhagyesh
i would suggest this one:
[val, idx] = find(a==8);
It seems to make a great job! :)
Cheers
Florian
Walter Roberson
on 15 Jan 2020
This will not return value and index, it will return row and column numbers.
Sorne Duong
on 21 Jul 2021
0 votes
a = 1, 3, 6, 9, 10, 15
We know the fourth value is 9, but how to find the fourth value in MATLAB?
4 Comments
a = [1, 3, 6, 9, 10, 15]
a(4)
Unless you mean how to predict the 4th value -- that is, how to find a rule or formula for the values.
Legendré (and others) proved that for any finite set of finite values, that there is a polynomial that goes through all of the values exactly (to within computation error.) So you can construct a polynomial that goes through all of the points. There are an infinite number of expressions that can be used... but because any list of values can be fit, if what you were given was
a = [1, 3, 6, X, 10, 15]
and you were asked to figure out what X is, then the technique of constructing polynomials cannot decide -- there are an infinite number of polynomials that go through (1,1), (2,3), (3,6), (5,10), (6,15) and an infinite number of them have a different value at X = 4. There is a polynomial for [1,3,6,-8,10,15]. There is a polynomial for [1,3,6,10,10,15]. There is a polynomial for [1,3,6,72432015,10,15] .
Because of this, if the question is to figure out what X is given a = [1, 3, 6, X, 10, 15] then the answer has to be "It could be any finite number".
Reem RA
on 30 May 2022
Hi Walter, I used the find function to find "positions" of values in a table colum. How do I get the values of those positions (values on int_t_03)? my code is bellow:
% determine the significant time interval for the bikes (3 sec before/after)
tstart_bikes_T_03 = min(bikes_T_03(:,4:4))-75
tend_bikes_T_03 = max(bikes_T_03(:,4:4))+75
% find road users that intersect with bikes time interval
int_t_03 = find(T_03(:,4)>= tstart_bikes_T_03 & T_03(:,4)<= tend_bikes_T_03);
Torsten
on 30 May 2022
Try
values = T_03(int_t_03,4)
Reem RA
on 30 May 2022
Thanks it worked!!!!
Nilesh Kumar Bibhuti
on 15 Oct 2021
Edited: Walter Roberson
on 15 Oct 2021
BRO , THIS WILL GIVE U THE DESIRED OUTPUT . HAPPY CODING :)
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE] ,brr[MAX_SIZE] ;
int size, i, toSearch, found ,k=0;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);
/* Input elements of array */
printf("Enter elements in array: ");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
for (i = 0; i < size; i++)
{
if (arr[i] == toSearch)
{
brr[k] = i+1 ;
printf("%d ",brr[k]);
k++ ;
}
}
return 0;
}
1 Comment
Walter Roberson
on 15 Oct 2021
In C, int size should really be size_t size and your scanf() should be using %lu instead of %d . i and k should also be size_t
You should be checking the return status of each scanf() call .
Also
int main()
should be
int main(void)
unless you are using K&R C from before C was standardized.
The user is expecting the positions to be returned, rather than displayed.
You probably shouldn't be assuming integer for the array, but that would be an acceptable limitation if specifically documented.
You do not use or initialize found.
Korosh Agha Mohammad Ghasemi
on 25 Jun 2024
Moved: Voss
on 25 Jun 2024
To find the positions of the value 8 in the vector a = [7 8 8 2 5 6], you can use the find function in MATLAB. Here's how you can do it:
a = [7 8 8 2 5 6];
% Find the positions of the value 8
positions = find(a == 8);
disp('Positions of value 8:');
disp(positions);
This code will output the indices of the elements in a that are equal to 8. For the given vector, it will display 2 and 3.
If you need the positions in a 1-based index format like (1,2) and (1,3) (assuming a is a row vector), you can directly use the indices obtained from the find function since MATLAB indexing starts from 1.
Here's the complete example:
a = [7 8 8 2 5 6];
% Find the positions of the value 8
positions = find(a == 8);
% Display the positions in (row, column) format
for i = 1:length(positions)
fprintf('(1,%d)\n', positions(i));
end
This script will output:
(1,2)
(1,3)
This clearly indicates the positions of the value 8 in the vector a.
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!