Effacer les filtres
Effacer les filtres

How to find the position of a number in an array?

1 836 vues (au cours des 30 derniers jours)
Arnab Pal
Arnab Pal le 15 Fév 2018
Déplacé(e) : Voss le 25 Juin 2024 à 17:02
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).

Réponse acceptée

Walter Roberson
Walter Roberson le 15 Fév 2018
Modifié(e) : MathWorks Support Team le 27 Fév 2020
You can use the “find” function to return the positions corresponding to an array element value. For example:
a = [7 8 8 2 5 6];
linearIndices = find(a==8)
linearIndices =
2 3
To get the row and column indices separately, use:
[row,col] = find(a==8)
row =
1 1
col =
2 3
If you only need the position of one occurrence, you could use the syntax “find(a==8,1)”. You can also specify a direction if you specifically want the first or last occurrence, such as “find(a==8,1,’first’). For more information on these options, see find.
  2 commentaires
Arvind Andrew Das
Arvind Andrew Das le 19 Juil 2022
i did the find function, but it gives me the ans vector with all of the values of the array and the results are in true or false for my condition( 0 & 1 ).
Walter Roberson
Walter Roberson le 20 Juil 2022
find() can never return 0. Perhaps you are looking at ans for a different operation?

Connectez-vous pour commenter.

Plus de réponses (4)

Bhagyesh Shiyani
Bhagyesh Shiyani le 5 Déc 2019
what if i want both 8 positions, any code?
  2 commentaires
Florian Reinbold
Florian Reinbold le 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
Walter Roberson le 15 Jan 2020
This will not return value and index, it will return row and column numbers.

Connectez-vous pour commenter.


Sorne Duong
Sorne Duong le 21 Juil 2021
a = 1, 3, 6, 9, 10, 15
We know the fourth value is 9, but how to find the fourth value in MATLAB?
  4 commentaires
Torsten
Torsten le 30 Mai 2022
Try
values = T_03(int_t_03,4)
Reem RA
Reem RA le 30 Mai 2022
Thanks it worked!!!!

Connectez-vous pour commenter.


Nilesh Kumar Bibhuti
Nilesh Kumar Bibhuti le 15 Oct 2021
Modifié(e) : Walter Roberson le 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 commentaire
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.


Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi le 25 Juin 2024 à 16:42
Déplacé(e) : Voss le 25 Juin 2024 à 17:02
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by