Replace double numbers in a matrix with new ones
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposte (1)
Walter Roberson
il 6 Ago 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 Commenti
Image Analyst
il 7 Ago 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
il 7 Ago 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)));
Vedere anche
Categorie
Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!