When faced with the same value in a vector, what can you do to choose one over the other?

1 vue (au cours des 30 derniers jours)
This may be a little hard to explain but I'll try my best.
So I have indexed a 3x1 vector(A) and 3x6 matrix(B) so that the location of the max number in vector A will be the base of location for bringing over all 3 values of A to the designated row that the max number is in. That code looks like
idx1 = (A==max(A));
B(idx1,1:3) = A
So let's say that creates
A = 1
5
2
B = NaN NaN NaN NaN NaN NaN
1 5 2 NaN NaN NaN
NaN NaN NaN NaN NaN NaN
Now if I had a tie between max numbers, what can I do to randomly create 2 new numbers to replace those same values so that I can do what I just did if there were no double max numbers? Like if A = [5;5;2] I would want to randomly change the two values of 5 into two new values between the value of 1:5 (let's say the former is changed to 1 and the latter is changed to 4). Then the 4 would be the new max number, and all previous coding (from when all values were different) would apply.
What lines of codes would work for this? I have tried a few if statements but none work.

Réponse acceptée

Stephen23
Stephen23 le 17 Fév 2016
Modifié(e) : Stephen23 le 17 Fév 2016
I would use randperm:
%A = [1;5;2]
A = [5;5;2]
B = NaN(3,6);
% new code:
idx = A==max(A);
num = nnz(idx);
A(idx) = max(A)-num+randperm(num,num);
% original code:
idy = A==max(A);
B(idy,1:3) = A
Note that this works for one or more maximum values. The code does not randomly pick new values from 1:5 as you ask, but instead uses randperm on the set of values [max,max-1,...]. This is because if the values are randomly picked then there is no guarantee that the new values will not cause multiple new max values, eg:
>> A = [5;5;4];
>> new = [4,1]; % random between 1:5
>> A(A==max(A)) = new
A =
4
1
4
and thus you get back to the same problem as you had at the start. For this reason I used randperm.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by