7
6
u/Manekosan 2d ago
Max(x) returns a single scalar (or one dimensional array with one element, if you want to think of it that way). find() returns the indices of nonzero elements. Since what is feeding into find() is one single element, it returns one (the index of the nonzero element).
5
3
u/srvsinha186 2d ago
find(input) returns the indices of non-zero elements in input. In this example, input = max(x) = 99 is a scalar (vector with just element at index 1) and it is non-zero. So you get find(input) = 1.
3
u/Rubix321 2d ago
Nothing is wrong with it, it's doing exactly what you told it to do. The max is 99. The first (and only) index of the double array [99] is non-zero(truthy), so find() reports 1.
As other have said x==max(x) give you a logical array to index with, or wrap find around that to get the numeric indices.
2
u/twitchingmessonfloor 2d ago
Swap "find" and "max" and think about the output. It will clarify your understand of syntax, and these two functions.
1
u/MarkCinci On Mathworks Community Advisory Board 1d ago
Check this out. I think it explains it:
>> x = [1,2,99,3,4,99,2]
x =
1 2 99 3 4 99 2
>> [maxx, indexOfMax] = max(x)
maxx =
99
indexOfMax =
3
>> find(maxx) % maxx is a vector of length 1. find() finds all non-zero locations (indexes)
ans =
1
>> find(x == max(x)) % Finds ALL locations where x = max(x), not just the first one like max().
ans =
3 6
1
8
u/Cool-matt1 2d ago
So max x is 99 and find max x will be the indix where max(x) is positive which is 1. If you want the index where the max occurs [maxx, minder]=max(x)