How can I randomize positions with retract/replacement and save in a text file?

1 visualización (últimos 30 días)
I would like to determine several target positions in the Z direction (height) for N=30. Possible values are 20,40,60,80,100,120,140 in the Z direction, i.e. a total of 7 positions. 10 different spatial positions with these numbers are to be determined with replacement (so there are a total of 70 possible positions). It should be drawn again if the same value is drawn twice in a row. For each N I need 40 target positions (10 per condition).
The target positions are to be saved in a text file with 40 columns and 30 lines (all values separated by commas).
I thank you for every suggestion!

Respuesta aceptada

Voss
Voss el 18 de En. de 2022
It sounds like you want a 30-by-40 matrix where each element is one of {20,40,60,80,100,120,140} and no two adjacent elements on a row are the same (i.e., draw again if the same value is drawn twice in a row).
N = 30;
M = 40;
vals = [20,40,60,80,100,120,140];
n_vals = numel(vals);
idx = reshape(mod(randperm(N*M)-1,n_vals)+1,N,M);
same_idx = [false(N,1) diff(idx,1,2) == 0];
while nnz(same_idx) > 0
idx(same_idx) = mod(randperm(nnz(same_idx))-1,n_vals)+1;
same_idx = [false(N,1) diff(idx,1,2) == 0];
end
writematrix(vals(idx),'output.txt','Delimiter',',');
% check the output file:
fid = fopen('output.txt');
result = char(fread(fid)).';
fclose(fid);
display(result);
result =
'60,120,20,140,120,140,80,100,40,60,100,80,140,80,20,60,140,40,140,80,20,100,140,120,40,80,60,120,40,20,100,40,120,140,120,140,60,120,60,140 100,140,60,120,60,40,60,80,20,140,80,20,60,100,40,120,100,40,100,80,60,140,60,100,60,140,20,140,120,100,20,80,120,60,120,100,140,120,100,80 80,100,20,140,80,100,80,40,80,120,80,120,60,80,140,40,20,120,40,100,120,100,60,40,120,40,20,80,100,120,20,100,20,140,20,60,120,140,120,100 80,100,40,60,120,100,60,120,60,140,20,120,60,140,80,40,140,60,20,120,80,60,40,140,20,40,100,80,140,60,20,60,100,40,60,100,80,120,60,140 40,80,140,60,140,60,140,20,60,120,140,120,20,40,120,20,120,140,100,60,40,100,40,20,140,20,120,80,120,60,40,80,140,60,40,140,60,40,20,80 40,100,40,80,60,140,40,120,20,120,60,120,40,100,20,120,40,20,100,80,120,100,20,120,20,40,100,60,100,80,20,140,40,80,140,120,40,120,100,20 120,100,40,60,100,60,100,80,40,20,80,120,80,60,80,60,120,100,140,20,100,140,80,20,80,100,120,60,120,80,100,20,40,140,40,100,20,100,40,100 140,40,140,40,60,20,140,120,20,140,40,120,100,120,40,140,20,60,120,20,60,20,100,80,100,60,20,120,40,20,100,120,140,80,100,120,80,120,80,40 40,60,100,20,120,140,40,120,20,80,60,40,140,60,100,40,20,100,80,20,120,100,80,120,60,140,40,80,40,100,60,100,120,40,80,40,120,20,140,60 140,100,140,20,60,120,80,40,100,80,40,20,100,120,40,80,140,40,20,100,120,60,20,100,140,80,60,80,120,100,140,120,100,80,140,40,120,60,100,140 120,80,140,100,60,100,80,140,40,140,40,120,60,100,20,40,60,140,60,20,40,100,120,40,120,20,120,40,80,20,100,140,40,20,80,60,40,20,100,20 140,40,20,80,20,60,20,40,100,140,20,100,40,120,80,140,20,40,60,140,20,100,20,120,20,80,120,100,40,120,20,60,140,20,140,120,80,100,120,100 60,140,120,80,40,80,140,40,140,60,20,40,80,40,100,80,60,100,40,20,100,120,40,20,100,60,120,100,120,60,20,40,100,20,100,20,40,140,40,80 140,80,20,140,100,80,40,140,60,100,60,80,140,80,20,80,20,60,100,60,80,20,40,80,60,20,40,120,140,120,140,100,40,100,40,20,60,140,40,140 40,140,60,120,20,120,80,40,80,100,60,120,60,40,140,60,100,120,60,80,100,20,140,20,100,140,60,120,100,60,20,140,120,40,80,140,80,140,40,140 100,40,20,80,140,120,80,120,60,80,140,120,100,20,140,120,100,120,80,40,60,40,140,100,140,20,100,60,40,80,140,120,100,140,40,120,140,60,120,40 40,120,20,120,100,140,100,40,140,60,100,20,80,60,100,40,100,60,100,80,20,120,20,40,80,40,100,120,100,60,40,80,40,100,140,20,140,120,60,120 140,120,140,100,120,100,120,60,140,100,60,20,40,60,140,100,60,40,60,20,60,20,100,120,40,120,20,80,20,100,40,80,100,20,100,120,60,120,80,40 80,140,20,80,120,100,80,20,140,60,40,120,100,80,100,40,120,100,20,140,100,140,40,100,120,20,40,80,140,80,60,120,140,20,100,60,40,120,60,20 60,80,60,40,80,100,140,100,20,140,20,100,80,100,140,100,40,60,80,120,80,20,40,20,100,120,20,100,140,120,140,80,100,40,20,40,80,100,60,120 80,140,120,140,120,80,140,100,140,20,80,40,20,80,20,120,80,120,100,120,40,100,20,100,60,40,20,120,80,120,40,80,120,80,60,40,60,100,40,60 140,100,120,60,20,80,100,140,120,40,60,80,40,80,140,60,140,20,60,120,80,100,40,140,80,140,120,60,80,100,120,20,120,60,140,20,80,100,120,40 120,140,20,60,80,60,120,60,100,80,140,80,100,60,100,60,40,20,140,60,120,100,40,80,140,20,40,140,100,80,60,140,20,80,40,20,140,120,80,120 120,100,120,60,80,120,100,60,80,40,140,20,140,20,120,140,20,140,100,80,20,40,60,80,140,40,60,120,80,140,40,60,40,20,120,60,80,140,100,120 100,20,80,60,40,100,40,100,80,60,20,80,120,60,120,60,20,100,140,120,60,140,60,40,80,40,120,20,60,80,100,140,20,60,20,60,80,120,100,120 120,80,40,140,20,120,60,80,100,140,80,60,40,100,60,40,60,80,40,80,20,100,60,100,20,100,60,80,140,80,40,20,40,140,120,100,80,140,60,80 120,140,20,40,20,40,60,80,60,100,140,60,20,120,140,120,40,60,140,80,60,120,140,40,20,140,120,40,80,20,100,80,60,20,80,120,60,20,40,20 20,80,120,20,80,40,140,120,140,40,60,100,120,80,20,40,60,120,80,60,100,120,40,140,120,80,20,80,120,80,60,120,20,40,60,120,80,100,40,20 60,20,40,120,100,40,20,80,140,60,100,140,80,140,80,140,60,80,120,40,80,20,120,140,120,100,20,100,140,60,140,60,80,140,40,20,80,140,100,80 100,20,80,40,60,20,120,140,120,60,100,20,140,60,120,20,100,60,120,60,120,60,120,140,120,100,60,80,40,60,80,120,140,120,20,40,140,80,20,80 '

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by