Replace double numbers in a matrix with new ones
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2023-8-6
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 个评论
Image Analyst
2023-8-7
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
2023-8-7
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)));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!