Replace double numbers in a matrix with new ones

4 vues (au cours des 30 derniers jours)
Susu
Susu le 6 Août 2023
Commenté : Walter Roberson le 7 Août 2023
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.

Réponses (1)

Walter Roberson
Walter Roberson le 6 Août 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 commentaires
Image Analyst
Image Analyst le 7 Août 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 le 7 Août 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)));

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by