MATLAB Answers

could anyone help me how to generate two different random numbers of same value.

1 view (last 30 days)
jaah navi
jaah navi on 5 Jul 2021
Commented: jaah navi on 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 Comments
jaah navi
jaah navi on 5 Jul 2021
Yes, I know that some of the values in A can be repeated in B.
In my case, the size of A and B is very large i.e, (48000,1). When I generate i can find only minimum number of values remains to be same i.e, fo example 2300 out of 48000. But I want the maximum number of values to be same i.e, 45700/48000.
Could you please hlp me onthis.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 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
A = 5×5
0.9605 0.1936 0.1763 0.7655 0.5840 0.6432 0.6447 0.2002 0.5192 0.1195 0.8839 0.0211 0.4184 0.5463 0.9373 0.2247 0.7324 0.8192 0.9718 0.4654 0.3539 0.5847 0.8463 0.1017 0.9999
A1
A1 = 5×5
0.3280 0.1936 0.1763 0.7655 0.7112 0.6432 0.6447 0.2002 0.5192 0.7247 0.8839 0.9045 0.3648 0.5463 0.9373 0.2247 0.7324 0.8192 0.9718 0.4654 0.4195 0.5847 0.8463 0.1017 0.9999
A2
A2 = 5×5
0.9605 0.1936 0.1763 0.7655 0.5840 0.7112 0.6447 0.2002 0.5192 0.1195 0.8839 0.7247 0.4184 0.5463 0.9373 0.2247 0.9045 0.3280 0.9718 0.4195 0.3648 0.5847 0.8463 0.1017 0.9999
B
B = 5×5
0.3280 0.3758 0.7772 0.0375 0.7112 0.4655 0.5999 0.1197 0.2314 0.7247 0.4005 0.9045 0.3648 0.0259 0.9559 0.6848 0.5505 0.0491 0.4264 0.4717 0.4195 0.3723 0.9590 0.2997 0.9910
Where did A1 flip?
[sort(locationOfMatchesInB).', find(A1 ~= A)]
ans = 6×2
1 1 5 5 8 8 13 13 21 21 22 22
What changed?
A1(A1 ~= A)
ans = 6×1
0.3280 0.4195 0.9045 0.3648 0.7112 0.7247
B(A1 ~= A)
ans = 6×1
0.3280 0.4195 0.9045 0.3648 0.7112 0.7247
  2 Comments
Walter Roberson
Walter Roberson on 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);

Sign in to comment.

More Answers (2)

Amit Bhowmick
Amit Bhowmick on 5 Jul 2021
Edited: Amit Bhowmick on 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))

Walter Roberson
Walter Roberson on 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
ans = 95.2233
p * 100
ans = 95.2083
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 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by