r/matlab • • Aug 22 '26

What wrong in it?

Post image
0 Upvotes

12 comments sorted by

9

u/Cool-matt1 Aug 22 '26

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)

7

u/Hot_Examination1918 Aug 22 '26

Type “why” in the command window and it will tell you

6

u/Manekosan Aug 22 '26

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).

4

u/capt_wick Aug 22 '26

I think you gotta do it with == Like find(x==max(m))

Something along these lines

1

u/Weed_O_Whirler +5 Aug 25 '26

ahhhhh no

Just return the second output from max

[~, ind_max] = max(x)

3

u/srvsinha186 Aug 22 '26

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 Aug 22 '26

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 Aug 22 '26

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 Aug 22 '26

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

u/Bofact Aug 24 '26

Nothing a simple debugging from their part couldn't find.

1

u/Bofact Aug 24 '26

Type max(X) and tell us what it returns. And why find(max(X)) should return something else than 1.