Array Mode Max Find

I need to consider the case in which the maximum is a mode.
With
A = [1 1 1 1 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7];
I need to get the indices for the max, which I have tried and it worked but suppose I have repeated minimum and for some reason it gives me more indices than needed.
So as I said, so oink should be returning:
6 13 20 27
but instead I get
1 6 8 13 15 20 22 27
[m,f,c] = mode(A); Tmode = max(c{1})
Tmax = max(A); if Tmax == Tmode
oink = find(diff(A)==0); % <----- the problem occurs here
test_idx = [1;oink];
else
label = [find(A==max(A))];
test_idx = [1;label];
end
The last condition doesn't pose a problem, it's the first. The problem would arise if I have both two repeated min and max. find(diff(A)==0) would return both. Is there a way around this?
Now suppose I have a vector with distinct elements A = [ 1 2 3 4 5 6]
c would return as the entire array. This is not what I want.
It would fail from :
[m,f,c] = mode(A);
Tmode = max(c{1})
If I had just taken Tmode = mode(A) then it would work but not progress to the first case.
Any advice?

3 Commenti

Jan
Jan il 14 Mar 2013
It is hard to fix a failing code, when the inputs, the wanted procedure and the outputs are not defined clearly. What is "the largest max element"?
T
T il 14 Mar 2013
See my comment to your post below.
Jan
Jan il 18 Mar 2013
Here you see the next problem, Anthony: There is a bunch of comments and it is not clear where to search. Therefore it is recommended, to write all useful information to the original question by editing it. Marking the additions by "[EDITED]" let others know, where new informations are added.

Accedi per commentare.

 Risposta accettata

Jan
Jan il 14 Mar 2013
I'm not sure if I understand the question. Are you looking for:
A = [1 1 3 5 6 7 7];
[value, index] = max(A)
?

8 Commenti

T
T il 14 Mar 2013
Modificato: T il 14 Mar 2013
Not quite. Perhaps that was a bad example.
Suppose I have:
A = [1 1 3 5 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7];
Tmax = max(A);
[m,f,c] = mode(A); % if there are, then check to see if there are repeated min and max.
Tmode = max(c{1}) % Sometimes there are repeated maximum, we only need one.
if Tmax == Tmode %so for cases of repeated maximum, find the indices at which these occur.
%Tmode = mode(A);
oink = find(diff(A)==0); % this does not take into account repeated minimum, needs fixing!
test_idx = [1;oink];
else
label = [find(A==max(A))];
test_idx = [1;label];
end
everything under else is fine.
T
T il 14 Mar 2013
Modificato: T il 14 Mar 2013
This is what I had initially, but mode(A) only selects the smallest repeated element. So the first condition will not satisfy
A = [1 1 3 5 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7];
if Tmax == Tmode
oink =find(diff(A)==0);
test_idx = [1;oink];
else
label = [find(A==max(A))];
test_idx = [1;label];
end
T
T il 14 Mar 2013
Modificato: T il 14 Mar 2013
The result test_idx should return
6 13 20 27.
As you can see, oink returns:
1 6 8 13 15 20 22 27
Which is not what I want.
Maybe I need to spend more time studying this, but you're starting to lose me. Do you need the mode (most common value), or the max (value with highest, greatest value)? In your case, 7 is both the max and the mode so it's getting confusing. What do you want in this case:
A = [1 1 1 1 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7];
where 1 is the mode and 7 is the max?
T
T il 14 Mar 2013
Modificato: T il 14 Mar 2013
I need to consider the case in which the maximum is a mode.
With
A = [1 1 1 1 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7 1 1 3 5 6 7 7];
I need to get the indices for the max, which I have tried and it worked but suppose I have repeated minimum and for some reason it gives me more indices than needed.
So as I said, so oink should be returning:
6 13 20 27
but instead I get
1 6 8 13 15 20 22 27
T
T il 14 Mar 2013
The error lies with this line:
oink = find(diff(A)==0);
T
T il 14 Mar 2013
Actually the code fails if I have the following example
A = [ 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ]
It will make Tmode = 5 and Tmax is obviously 5 and won't go the second case even though the elements are in sequence
Jan
Jan il 18 Mar 2013
Modificato: Jan il 18 Mar 2013
I still do not get what you are searching for. Are you looking for the most frequent element, and when there are multiple of them you want the largest one? Or does the order of elements matter? I assume one single sentence would clarify the question immediately. And please add this by editing the original question, such that other reader do not have to dig in a pile of comments. Thanks.

Accedi per commentare.

Più risposte (0)

Tag

Richiesto:

T
T
il 14 Mar 2013

Community Treasure Hunt

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

Start Hunting!

Translated by