Effacer les filtres
Effacer les filtres

How can I assign the same value to a repeating number?

6 vues (au cours des 30 derniers jours)
giancarlo maldonado cardenas
atachments files:
data.mat = is a list
data.mat = is the workspace
I have a list data.mat, within this list there is a field called "Preamble".
then in the for what I do is assign a random number from the variable "re" which is a vector [1 64] to the list allocated in a new field called "prb".
my problem is that in the crazy list, in the {1,6} and {1,7} the variable "Preamble" repeats the number 50. But the value of "prb" was assigned to the {1,6} = 60 and at {1,7} the value of "prb" = 4.
what I want is that if two repeated numbers are found in "Preamble" the same value of "prb" is assigned.
for example:
we know that {1,6} and {1,7} the variable "Preamble" repeats the number 50.
so the "prb" assigned to {1,6} and {1,7} should be "prb" = 60.
(remembering that all these numbers are random) that is, the number that is repeated cannot always be 50, or the repeated number can change position, that is {1,3} and {1,1} or any other random position.
this is my code.
Thanks in advance
clear all
prb = 64;
for ca = 1:length(datos)
entry = re(randi(length(re)));
alocados{ca}.Prb= entry;

Réponse acceptée

AndresVar le 17 Fév 2022
(1) Not sure why "alocados" is just a random order of "datos" but you can do this any time, before or after Prb is assigned right?
(2) If "Preambulo" has N unique values, then you need N random values:
Preambulo = [43 4 26 16 61 50 50 46];
[~,ia,ic] = unique(Preambulo) % see how many unique values and their mapping
ia = 7×1
2 4 3 1 8 6 5
ic = 8×1
4 1 3 2 7 6 6 5
randomNumbers = randi(64,[1,numel(ia)]) % a list of random numbers up to 64
randomNumbers = 1×7
5 14 37 45 49 28 55
Prb = randomNumbers(ic) % map Preambulo to the list of random values
Prb = 1×8
45 5 37 14 55 28 28 49

Plus de réponses (1)

Walter Roberson
Walter Roberson le 17 Fév 2022
Use the two output form of findgroups, or the third and first outputs of unique(). Either way you get a list of unique values, and a list of group identifiers, with all the values with the same identifier matching to the same value.
Now randperm on the number of unique elements and index the unique values by the random permutation, so that you get a scrambled list of unique values. Now take the identifiers and use them to index the scrambled list. The result will have the same property as the original, in that two locations that originally had the same value, will be the same as each other afterwards.


En savoir plus sur LTE Toolbox 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