Replace double numbers in a matrix with new ones

3 views (last 30 days)
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
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
Image Analyst
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
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)));

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices 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!