mercredi 11 avril 2018

how to simultaneously satisfy conditions per rows and columns in Matlab

I have a table:

%% Example data
ID = [1:40]';
ID = table(ID);

myn=10 ; myx=(1:4)';
Trial = repmat(myx', myn, 1);
Trial = Trial(:);
Trial = table(Trial);

Rx=['a';'b';'c';'d']; Rn=10;
Route = [repmat(Rx(1),Rn,1);repmat(Rx(2),Rn,1);repmat(Rx(3),Rn,1);repmat(Rx(4),Rn,1)];
Route = table(Route);

Sn=4 ; Sx=(1:10)';
Scene = repmat(Sx, Sn, 1);
Scene = Scene(:);
Scene = table(Scene);

myTarget=[0;0;0;0;0;0;1;0;0;0;0;0;1;0;0;0;0;1;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;];
myTarget = table(myTarget);

my_table = [ID Trial Route Scene myTarget];

and I would like to create 24 new columns: Part1, Part2, Part3, etc. until Part24, such that:

*if myTarget==1, then in each column (PartX) I need a value of 1 in the corresponding row (i.e., across the 24 new columns (PartX) I have a total of twenty-four 1s);

*if myTarget==0, then in each column (PartX) I need to have a value of 0 or 1 such that:

1) the sequence of 1s and 0s is random across the 24 new columns,

2) for each Trial and Route, I have a total of five 1s and five 0s,

3) across the 24 new columns (PartX) I have a total of twelve 1s and twelve 0s.

I would like to have something like this:

Scene   myTarget    Trial   Route   Part1   Part2   Part3   Part4   …   Part24  Tot1    Tot0
1       0           1       A       0       1       1       0           0       12      12
2       0           1       A       1       0       1       0           0       12      12
3       0           1       A       1       0       0       0           0       12      12
4       0           1       A       0       1       0       1           0       12      12
5       0           1       A       1       0       1       1           0       12      12
6       0           1       A       1       0       0       0           1       12      12
7       1           1       A       1       1       1       1           1       24      0
8       0           1       A       0       0       1       1           1       12      12
9       0           1       A       0       1       1       1           1       12      12
10      0           1       A       0       1       0       0           1       12      12

I am trying to use these two codes, but I am not sure how to satisfy the above conditions:

%% The following code takes into account having twelve values of 1s and twelve values of 0s in each row,
% but it doesn't account for having five 1s and five 0s for each trial and route.
% Set up parameters.
rows = 40;
columns = 24;
onesPerRows = 12;
% Initialize matrix.
m = zeros(rows, columns);
for rws = 1 : rows
    % Get random order of columns .
    randColumns = randperm(columns);
    % Pick out "onesPerRows" rows that will be set to 1.
    columnsWithOne = randColumns(1:onesPerRows);
    % Set those rows only to 1 for this column.
    m(rws, columnsWithOne) = 1;
end

%% The following code takes into account having five values of 1s and five values of 0s for each trial and route,
% but it doesn't account for having twelve 1s and twelve 0s in each row.
% Set up parameters.
my_table.myRoute = cellstr(my_table.Route);
G= findgroups(my_table.Trial,my_table.myRoute);

rows2 = 10;
columns2 = 1;
onesPerColumns = 5;
p = zeros(rows2, columns2);
for col = 1 : columns2
    % Get random order of rows .
    randRows = randperm(rows2);
    % Pick out "onesPerColumns" rows that will be set to 1.
    rowsWithOne = randRows(1:onesPerColumns);
    % Set those rows only to 1 for this column.
    p(rowsWithOne,col) = 1;
end

Aucun commentaire:

Enregistrer un commentaire