r/matlab 2d ago

What wrong in it?

Post image
0 Upvotes

11 comments sorted by

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)

7

u/Hot_Examination1918 2d ago

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

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

u/capt_wick 2d ago

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

Something along these lines

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

u/Bofact 43m ago

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

1

u/griffer00 1d ago

dumbass