How can I assign the same value to a repeating number?
2 次查看(过去 30 天)
显示 更早的评论
giancarlo maldonado cardenas
2022-2-17
编辑: giancarlo maldonado cardenas
2022-2-17
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;
%*********************************
re=1:prb;
for ca = 1:length(datos)
entry = re(randi(length(re)));
alocados{ca}.Prb= entry;
end
0 个评论
采纳的回答
AndresVar
2022-2-17
(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
randomNumbers = randi(64,[1,numel(ia)]) % a list of random numbers up to 64
Prb = randomNumbers(ic) % map Preambulo to the list of random values
0 个评论
更多回答(1 个)
Walter Roberson
2022-2-17
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!