Dear all,
I have a cell array, in which each cell is a vector of integers of potentially different length. I would like to change the values of several randomly chosen elements of this cell array, replacing them by a numbers from a look-up table. My code looks like this:
p_mut = 0.2;
R = cell(2,N);
R{1,1} = [1 2 4 5]; R{1,2} = [1 3 4]; etc ...
Mut_trajectory is a matrix with 4 columns. The numbers in the i'th row could replace the value i, if it is the one chosen. For example: Mut_trajectory = [ 121 38 51 203; 147 203 121 202; 118 114 139 151; 220 191 214 166; 20 70 64 179; ....];
I defined the following anonymous function: my_mut = @(x) x + (rand(size(x)) < p_mut).*(Mut_trajectory(x, randi(4,size(x))) - x);
This function randomly chooses elements of the cell with probability p_mut and then replaces each of them with one of the 4 elements (again chosen randomly) in the compatible row of 'Mut_trajectory'.
This only works properly if each cell in R is a scalar. For example: R =
[3] [2] [2] [2] [4] [4] [4] [5] [1] [1]
[5] [1] [5] [1] [1] [3] [1] [3] [4] [4]
R_mut = cellfun(my_mut, R, 'un',0)
R_mut =
[151] [ 2] [203] [121] [4] [4] [ 4] [5] [1] [203]
[ 5] [203] [179] [ 1] [1] [3] [121] [3] [4] [ 4]
Otherwise I get an error message:
"Error using - Matrix dimensions must agree.
Error in @(x)x+(rand(size(x))<p_mut).*(Mut_trajectory(x,randi(4,size(x)))-x)"
How should I define this properly for a general length of the vectors?
I'd be grateful for your help. I'm not very experienced with anonymous functions. I've been struggling with this for a while and failed to find advice in the Matlab help.
Thanks,
--Tamar.