help needed in defining anonymous function

1 view (last 30 days)
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.
  1 Comment
Tamar Friedlander
Tamar Friedlander on 5 Jan 2015
Hi,
I solved the problem. I redefined the anonymous function as:
my_mut = @(x) x + (rand(size(x)) < p_mut).*(Mut_trajectory(sub2ind(size(Mut_trajectory), x, randi(4,size(x)))) - x);
Now it works for a general size of vector.
Dear Stephen: thanks for the hint. I understood all that before writing my question - but that still didn't tell me how to remedy this, since I wasn't familiar with usage of 'sub2ind' as a means to get a list of matrix entries. I thought this forum is meant to help the less experienced users.
Anyway, seems like it's solved now.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 4 Jan 2015
Edited: Stephen23 on 4 Jan 2015
This has nothing to do with the fact it is an anonymous function, and everything to with the subtraction operation that you are performing. The fact that the error occurs while you are calling an anonymous function is irrelevant.
Read the error message carefully (they are there for a reason!): "Error using - Matrix dimensions must agree". So there is a problem with -, aka minus. Now read the documentation for minus, particularly this bit: "Inputs A and B must be the same size unless one is a scalar". Now check your calculation inside the anonymous function: you have something a bit like this (...)-x. As you point out, it works without error if the cell array R contains only scalar values, because in this case x is always a scalar, thus it fulfills the conditions explained in the documentation.
Now consider what happens if R contains values that are not scalars, then x is also not a scalar. Do we still fulfill the requirements of the documentation? Are both side of the minus operation the same size, or is (atleast) one a scalar, as the documentation requires?

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!