r/matlab 11d ago

What wrong in it?

Post image
0 Upvotes

12 comments sorted by

8

u/Cool-matt1 11d 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)

7

u/Hot_Examination1918 11d ago

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

6

u/Manekosan 11d 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).

4

u/capt_wick 11d ago

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

Something along these lines

1

u/Weed_O_Whirler +5 7d ago

ahhhhh no

Just return the second output from max

[~, ind_max] = max(x)

3

u/srvsinha186 11d 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 10d 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 11d 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 10d 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

u/Bofact 9d ago

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

1

u/Bofact 9d ago

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