Preventing repeated sequences in a matrix
Ältere Kommentare anzeigen
I have a 192 x 3 matrix, order(192 x 3):
order(:, 1) and order(:, 2) both contain repeating values of 1 - 16, and order(:, 3) contains repeating values of 1 and 2. I need to shuffle the matrix, while preventing any repeats of more than three of the same value in the last column, so order(:, 3) should never show more than 3 repeats of 1 or 2.
This is what I have, which worked for a smaller version of the matrix just fine, but seems to get stuck with a slightly larger matrix:
not_good = true;
while not_good
not_good = false;
order = Shuffle(order);
% returns an array of 1s and 0s indexing the position of the values for 1 and 2
R1 = order(:, 3) == 1;
R2 = order(:, 3) == 2;
% checks for repeats, returns 1 if repeats are present
rep_test1 = any(diff([1; find(R1)])>3);
rep_test2 = any(diff([1; find(R2)])>3);
if rep_test1 > 0 || rep_test2 > 0
not_good = true;
end
end
Any comments much appreciated. Thanks.
1 Kommentar
What is Shuffle?
>> help Shuffle
Shuffle not found.
And give the matrix order. Is this a good approximation of order?
order = cell2mat(arrayfun(@(x) randperm(x).',16*ones(12,2),'Un',0));
order = [order cell2mat(arrayfun(@(x) randperm(x).',2*ones(96,1),'Un',0))];
Akzeptierte Antwort
Weitere Antworten (1)
It will be much cheaper to shuffle the index vector only and use an UINT8 as long as less than 255 elements are processed (based on Matt's code):
index = uint8(1):uint8(192);
data = [mod(randperm(192), 16); mod(randperm(192), 16)].';
crit = mod(randperm(192), 2) + 1; % Avoid repeated transposition
T = true(1,4);
while 1 % Cheaper than the function(!) TRUE
index = Shuffle(index);
test = crit(index);
if isempty(strfind(test == 2, T))
if isempty(strfind(test == 1, T))
break
end
end
end
% Create the full data once only after the calculations:
result = cat(2, data(index, :), double(crit(index).'));
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!