How to find first instance of a value in array?

613 views (last 30 days)
Hi, I am writing a code that as follows:
arr(p)=abs(c); %absolute value of c
if(arr(p)<1)
if(arr(p)>0.9)
array(t)=white1;
cor(t)=arr(p); %correlation for c>.95 && c<1
t=t+1;
end
end
maximum=max(cor); %maximum value of cor(t)
Now, I am trying to find the first instance the value of maximum comes in the "arr" Array.
I have tried the following:
1.
for v=1:x
if(maximum==arr(v))
% x=find(arr==maximum);
u=u+1;
if(u==1) %making sure only the first max value is taken
q=value(v); %q is becoming always 0.5,have to fix it
end
end
end
and,
2.
x=find(arr==maximum);
and,
3.
x = find(maximum == arr,'first') ;
In the 1st and 2nd try, when I use fprintf to detect the x, it prints blank, such as,
"x is "
In the 3rd try the following error occurs,
Second argument must be a positive scalar integer.
How can I fix it?

Accepted Answer

Steven Lord
Steven Lord on 4 Jul 2021
You could use logical indexing.
rng('default')
c = randn(1, 10)
c = 1×10
0.5377 1.8339 -2.2588 0.8622 0.3188 -1.3077 -0.4336 0.3426 3.5784 2.7694
arr = abs(c)
arr = 1×10
0.5377 1.8339 2.2588 0.8622 0.3188 1.3077 0.4336 0.3426 3.5784 2.7694
% Overwrite those values in arr that are out of bounds with NaN. I'm using
% wider bounds to show the technique with a small data set.
arr(arr > 1.5) = NaN
arr = 1×10
0.5377 NaN NaN 0.8622 0.3188 1.3077 0.4336 0.3426 NaN NaN
arr(arr < 0.5) = NaN
arr = 1×10
0.5377 NaN NaN 0.8622 NaN 1.3077 NaN NaN NaN NaN
[value, location] = max(arr, [], 'omitnan')
value = 1.3077
location = 6
If all you're interested in is the value and not its location in c you could delete the elements of arr that fall out of bounds.
arr2 = abs(c);
arr2(arr2 > 1.5) = [];
arr2(arr2 < 0.5) = []
arr2 = 1×3
0.5377 0.8622 1.3077
[value2, location2] = max(arr2)
value2 = 1.3077
location2 = 3
location2 is not the same as location because arr2 does not have the same number of elements as either arr or c.

More Answers (2)

Jiro Doke
Jiro Doke on 4 Jul 2021
Is this what you're looking for?
maximum = 5;
arr = [2 4 3 5 7 3 4];
find(arr == maximum, 1)
ans = 4
  1 Comment
Tawsif Mostafiz
Tawsif Mostafiz on 4 Jul 2021
Edited: Tawsif Mostafiz on 4 Jul 2021
@jiro in this code:
arr(p)=abs(c); %absolute value of c
if(arr(p)<1)
if(arr(p)>0.9)
array(t)=white1;
cor(t)=arr(p); %correlation for c>.95 && c<1
t=t+1;
end
end
maximum=max(cor);
when I do this:
x=find(arr == maximum, 1);
It works for some input. It finds the first index of arr==maximum. However, when I print maximum with fprintf, for some input the x shows blank, such as:
x is
To investivage the problem, I printed arr(p) too. And found that the maximum I got from maximum=max(cor); is not present in the arr(p)
Let me give you the output:
Here's arr(p) in output:
4.595946e-02
2.556745e-02
6.488689e-02
6.076814e-02
8.913487e-03
6.457960e-02
1.031540e-01
1.213301e-02
5.870144e-02
1.425681e-01
1.397469e-03
1.525214e-01
1
5.967187e-02
7.100746e-02
7.474598e-02
1.066773e-01
1.259531e-01
7.464354e-01
7.420288e-01
7.217714e-01
4.300225e-01
3.690350e-01
3.326089e-01
3.141340e-01
1.852171e-01
1.726543e-01
1.494046e-01
1.428864e-01
1.347272e-01
1.168218e-01
1.118371e-01
9.120090e-02
8.713045e-02
5.995163e-02
6.033924e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
5.797939e-02
And here's maximum:
maximum is 9.682138e-01
As you can see, maximum does not match any arr(p) values.
But when I print cor(t), the value of maximum can be found in it. Among other values.
What I don't understand is that maximum is the maximum value of arr(p) when arr(p)>0.95 and <1, which is stored in cor(t). But when I do max(cor), why maximum value is not present in arr(p)?

Sign in to comment.


madhan ravi
madhan ravi on 4 Jul 2021
help find % read the third explanation
FIND Find indices of nonzero elements. I = FIND(X) returns the linear indices corresponding to the nonzero entries of the array X. X may be a logical expression. Use IND2SUB(SIZE(X),I) to calculate multiple subscripts from the linear indices I. I = FIND(X,K) returns at most the first K indices corresponding to the nonzero entries of the array X. K must be a positive integer, but can be of any numeric type. I = FIND(X,K,'first') is the same as I = FIND(X,K). I = FIND(X,K,'last') returns at most the last K indices corresponding to the nonzero entries of the array X. [I,J] = FIND(X,...) returns the row and column indices instead of linear indices into X. This syntax is especially useful when working with sparse matrices. If X is an N-dimensional array where N > 2, then J is a linear index over the N-1 trailing dimensions of X. [I,J,V] = FIND(X,...) also returns a vector V containing the values that correspond to the row and column indices I and J. Example: A = magic(3) find(A > 5) finds the linear indices of the 4 entries of the matrix A that are greater than 5. [rows,cols,vals] = find(speye(5)) finds the row and column indices and nonzero values of the 5-by-5 sparse identity matrix. See also SPARSE, IND2SUB, RELOP, NONZEROS. Documentation for find doc find Other functions named find codistributed/find gpuArray/find mtree/find tall/find

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!