Calculations with functions in cells

Hello I have the matrix d and I would like to calculate the minimum value in the first cell d{1}. But I have a mistake somewhere:
A1=rand(64,1);
A2=rand(2,2);
for k = 1:1:2
d{k}=@(z) (((A1(:,1)-A2(k,1)).^2)+((z-A2(k,2)).^2));
end
z0=[0,0];
fminsearch(d{1},z0)

8 Comments

Hello Spyros Polychronopoulos:
f(x) is a function that returns a scalar, and x is a vector or a matrix.
Your function d{k} returns a vector.
Kind regards,
Robert
Hi Robert Uhlig, Thank you for getting back to me. I don't think that that is the problem as this one works perfectly fine
A = [2,4,8,16];
for k = 1:numel(A)
C{k}=@(x)(x(k)-A(k)+5).^2;
end
fun = @(x)sum(cellfun(@(f)f(x),C));
x0 = [0,0,0,0];
fminsearch(fun,x0)
Or this one
A1=[5,8];
fun = @(x) sum((x-A1).^2);
x0 = [-1.2,1];
x = fminsearch(fun,x0)
Hello Spyros Polychronopoulos:
As your examples do not fit your erroneous solution, I suggest to try the following:
Example 1:
A = [2,4,8,16];
for k = 1:numel(A)
C{k}=@(x)(x(k)-A(k)+5).^2;
end
fun = @(x)sum(cellfun(@(f)f(x),C));
fun([0,0,0,0])
Result 1:
>> ans = 140
Example 2:
A1=[5,8];
fun = @(x) sum((x-A1).^2);
x0 = [-1.2,1];
fun(x0)
Result 2:
>> ans = 87.4400
Example 3:
A1=rand(64,1);
A2=rand(2,2);
for k = 1:1:2
d{k}=@(z) (((A1(:,1)-A2(k,1)).^2)+((z-A2(k,2)).^2));
end
d{2}(0)
Result 3:
>> ans = 0.5247
0.3243
0.3132
...
0.4571
As you can see (and should have seen from own trials) the first two examples output scalars whereas the third example gives a vector which is not supported by fminsearch().
Kind regards,
Robert
Hi Robert Uhlig, First of all thank you for coming back to me! As you know from the previous post I am looking to find a number of solutions for in-depended values that's why I was trying to have z1,z2,...,zn variables. But you proved that you get the same sets of solutions if you use one variable z. But I tried the below to get z1,z2,...,z64
A1=rand(64,1);
A2=rand(2,2);
for k = 1:1:2
for kk=1:1:64
eval(cat(2,'d{kk,k} = @(z',num2str(kk),') (((A1(kk,1)-A2(k,1)).^2)+((z',num2str(kk),'-A2(k,2)).^2));'));
end
end
Then adding them up (doesn't work with sum or plus)
for k = 1:1:2
d1{k}=sum(d{:,k});
end
And then the optimiser
z0=zeros(64,1);
fminsearch(d1{1,1},z0)
I was looking your 3rd Example. Could I get the sum and optimiser in the end? I don't see how am I going to sum up the columns that way because the final array is 1x2. I also tried to use the optimiser but didn't work:
A1=rand(64,1);
A2=rand(2,2);
for k = 1:1:2
d{k}=@(z) (((A1(:,1)-A2(k,1)).^2)+((z-A2(k,2)).^2));
end
z0=zeros(64,1);
fminsearch(d{1},z0)
"... I am looking to find a number of solutions for in-depended values that's why I was trying to have z1,z2,...,zn variables... But I tried the below to get z1,z2,...,z64."
Ugh, what a slow, complex, and buggy way to store data and write code.
"Then adding them up (doesn't work with sum or plus)"
Yep, that is true. And this is one of many reasons that all MATLAB experts will advise you to avoid magically creating or accessing lots of separate variables. The MATLAB documentation specifically warns against doing what you are trying to do: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
Once beginners learn to avoid using lots of numbered variables or magically trying to access variable names dynamically then their code will be simpler, neater, faster, more efficient, less buggy, and easier to debug. Read this to know more:
Thank you very much Stephen Cobeldick! But if I go for your Example 3 will I be able to run fminsearch of the summation afterwards? I will read the links you kindly sent! Thank you again!
Hi Spyros Polychronopoulos, did you find a solution for your question? I am having a similar problem.
I have 2 arrays (200x200 each) and trying to make a sum of functions.
Hi Juliana, I don't understand your issue. could you send here an example code?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!