- Do you want the sum of A is the same as the sum of B?
- Do you want exactly 3 (for examples) values in A to be already repeated in B?
could anyone help me how to generate two different random numbers of same value.
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
jaah navi
am 5 Jul. 2021
Kommentiert: jaah navi
am 6 Jul. 2021
I am having two matrices A and B generated randomly with different values
A=rand(4,3)
B=rand(4,3)
Now I want to know is there any other way such that the some of the values generated in A can be made equal to the values generated in B.
2 Kommentare
Yongjian Feng
am 5 Jul. 2021
Not very sure what you need here. Since rand is random, it is possible already some of the values in A can be repeated in B.
Akzeptierte Antwort
Steven Lord
am 5 Jul. 2021
A = rand(5, 5);
A1 = A; A2 = A; % Make copies for later comparison
B = rand(5, 5);
numberOfMatches = 6;
locationOfMatchesInB = randperm(numel(B), numberOfMatches);
If you want the elements from B to be in exactly the same locations in A:
A1(locationOfMatchesInB) = B(locationOfMatchesInB);
If you want the elements from B to be put in some location, but not necessarily the same locations, in A:
locationOfMatchesInA = randperm(numel(A), numberOfMatches);
A2(locationOfMatchesInA) = B(locationOfMatchesInB);
Now compare:
A
A1
A2
B
Where did A1 flip?
[sort(locationOfMatchesInB).', find(A1 ~= A)]
What changed?
A1(A1 ~= A)
B(A1 ~= A)
2 Kommentare
Walter Roberson
am 6 Jul. 2021
A = rand(5, 5);
A1 = A; A2 = A; % Make copies for later comparison
B = rand(5, 5);
locationOfMatchesInB = randperm(numel(B), 6);
Weitere Antworten (2)
Amit Bhowmick
am 5 Jul. 2021
Bearbeitet: Amit Bhowmick
am 5 Jul. 2021
Check the values you will alyas get zero that is they are not equal. More preciesly probability of getting same value is very less. Basically the algorithm of generating random number is such you can learn here
https://en.wikipedia.org/wiki/Random_number_generation
rand(4,3)==rand(4,3) or isequal(rand(4,3),rand(4,3))
0 Kommentare
Walter Roberson
am 5 Jul. 2021
A = sort(rand(300,300));
imagesc(A)
title('original')
p = 45700/48000;
mask = rand(size(A)) <= p;
B = [A(mask); rand(numel(A)-nnz(mask),1)];
B = reshape(B(randperm(numel(B))), size(A));
imagesc(B)
title('rerandom')
mean(ismember(B(:), A)) * 100
p * 100
So in this case, the target fraction to keep was 95.2083% and the actual fraction kept was 95.2233%
If it was necessary to have exactly the ratio 45700/48000 stay the same then that could be done... though it becomes trickier if you are working with integers.
3 Kommentare
Walter Roberson
am 6 Jul. 2021
A = arrayfun( @my_rand, repelem((1:10).',30), 'UniformOutput', false);
B = arrayfun( @my_rand, repelem((1:10).',30), 'UniformOutput', false);
p = 0.9;
nkeep = ceil(p * numel(A)) ;
keepidx = randperm(numel(A), nkeep);
B(keepidx) = A(keepidx);
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!