Replace double numbers in a matrix with new ones
Show older comments
How can I ensure that 2 matrices have numbers from 1 to 40. BUT the columns of the two matrices are different, so that the numbers that appear in the first matrix may not appear in the second matrix in the first column.
Therefore, I can check whether a number that is in the 1st column of the 1st matrix is also in the 1st column of the 2nd matrix and if this is the case, then this number (from the 1st column of the 2nd matrix) should be assigned another number that is not in the 1st column of the 1st matrix.
Answers (1)
Walter Roberson
on 6 Aug 2023
found = ismember(SecondMatrix(:,1), FirstMatrix(:,1));
now reassign Secondmatrix(found, 1)
... or, you know, you could do
temp = randperm(40);
FirstMatrix(:,1) = temp(1:20);
SecondMatrix(:,1) = temp(21:40);
Guaranteed that afterwards, no entry in SecondMatrix(:,1) appears in FirstMatrix(:,1)
11 Comments
Susu
on 6 Aug 2023
temp = randperm(40);
t1 = temp(1:20);
t2 = temp(21:40);
FirstMatrix(:,1) = t1;
FirstMatrix(:,2) = t2(randperm(20));
SecondMatrix(:,1) = t2;
SecondMatrix(:,2) = t1(randperm(20));
disp(FirstMatrix)
disp(SecondMatrix)
find(ismember(SecondMatrix(:,1), FirstMatrix(:,1)))
T1 = 1 : 40;
T2 = randperm(numel(T1),4)
T1(T2) = [];
T3 = reshape(T1(randperm(numel(T1))), 4, [])
That is, you can construct your arrays to not have any repetitions.
Susu
on 6 Aug 2023
T1 = 1 : 40;
T2 = randperm(numel(T1),4)
T1(T2) = [];
T3 = reshape(T1(randperm(numel(T1))), 4, [])
output = [T2(:), T3]
I don't know exactly how to filter for the number that appears in the same columns of the two matrices
My very first response was to show you how to use ismember() to do the filtering.
What I have been pointing out since then is that you can avoid that by strategic use of randperm and indexing. Pay attention to the way T3 was constructed: T1(T2) = []; on the line above removes from T1 all of the elements that were already used, and T1(randperm(numel(T1))) is then a random re-ordering of everything that is left over.
Susu
on 6 Aug 2023
Walter Roberson
on 6 Aug 2023
found = ismember(SecondMatrix(:,1), FirstMatrix(:,1));
if any(found)
SecondMatrix(found,1) = ReplacementValuesThatYouWouldCompute;
end
Loop not required. The ismember() will return a vector of values, one for each row in SecondMatrix. A value of true at a particular offset tells you that SecondMatrix(ThatOffset,1) is a duplicate of something inside FirstMatrix(:,1) . A value of false at a particular offset tells you that SecondMatrix row does not duplicate anything in the first column of FirstMatrix. It is not necessary to loop testing each row of SecondMatrix in turn.
Susu
on 6 Aug 2023
Image Analyst
on 7 Aug 2023
What is the use case? WHY do you want this thing? Is it homework, or is there some real world need for it?
Walter Roberson
on 7 Aug 2023
found = ismember(SecondMatrix(:,1), FirstMatrix(:,1));
SecondMatrix(found,1) = 0;
candidates = setdiff(1:40, SecondMatrix(:,1));
SecondMatrix(found,1) = candidates(randperm(numel(candidates), sum(found)));
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!