Matlab: function handle in cell array with undefined operators - matlab

When I try to run this code I get the following error: "Undefined operator '.*' for input arguments of type 'cell'." My goal here is to build an array (cell array since I'm working with function handles) via a for loop and take the integral of each element of the resulting array. The error occurs on the last line. I'm trying to plug in the value 1.5 for every element in the array. Any tips on how to "handle" this error?
FUN_1 = #(y_1,y_2,x_1,x_2)sum(heaviside(y_1-a_k(1:m,1)).*dirac(1,y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_1-y_1).^2./((x_1-y_1).^2)+sum(dirac(y_1-a_k(1:m,1)).*dirac(y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_1-y_1).*(x_2-y_2)./((x_1-y_1).^2+(x_2-y_2).^2);
Q_1 = #(x_1,x_2)integral2(#(y_1,y_2)FUN_1(y_1,y_2,x_1,x_2),a(1,1),c(1,1),a(1,2),c(1,2));
FUN_2 = #(y_1,y_2,x_1,x_2)sum(heaviside(y_1-a_k(1:m,1)).*dirac(1,y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_1-y_1).*(x_2-y_2)./((x_1-y_1).^2)+sum(dirac(y_1-a_k(1:m,1)).*dirac(y_2-a_k(1:m,2))).*(-1/2.*log((x_1-y_1).^2+(x_2-y_2).^2))+(x_2-y_2).^2./((x_1-y_1).^2+(x_2-y_2).^2);
Q_2 = #(x_1,x_2)integral2(#(y_1,y_2)FUN_1(y_1,y_2,x_1,x_2),a(1,1),c(1,1),a(1,2),c(1,2));
k = cell(1,2*M-1);
n=0;
for n = 0:2*M-1
k{1,n+1} = #(x_1,x_2)Q_1(x_1,x_2)*2*n*(x_1+1i*x_2)^(n-1)+ Q_2(x_1,x_2)*2*n*1i*(x_1+1i*x_2)^(n-1)]);
end
R = #(x_2)integral(#(x_1)k,a(1,1),c(1,1),'ArrayValued',true);
x= 1.5;
R{x}
I've updated the code as follows:
k = zeros(1,2*M);
n=0;
for n = 0:2*M-1
S = #(x_1,x_2)Q_1(x_1,x_2)*2*n*(x_1+1i*x_2)^(n-1) + Q_2(x_1,x_2)*2*n*1i*(x_1+1i*x_2)^(n-1);
R = #(x_2)integral(#(x_1)S,a(1,1),c(1,1));
k(1,n+1) = R(1);
end
disp(k);
but I'm still getting the following error:
"Input function must return 'double' or 'single' values. Found 'function_handle'.
for the line
k(1,n+1) = R(1);
Any tips?

In this line:
R = #(x_2) integral(#(x_1) S, a(1, 1), c(1, 1));
You aren't passing any values to S within your anonymous function #(x_1) S, so that anonymous function is just returning a function handle S instead of evaluating S for a set of inputs. I'm guessing you want to define it like this:
R = #(x_2) integral(#(x_1) S(x_1, x_2), a(1, 1), c(1, 1));

Related

how to convert second output parameter type for calling build-in function from double to single in matlab coder?

My code as follows:
[left,~] = ind2sub([size,size],finalIndex);
The warning message is following:
Output 2 of builtin function ind2sub is double-precision and has been cast to single-precision. The code generated for the builtin function may still contain doubles.
Line 9 give another error message:
[![Calls to builtin functions via function handles will not be converted to single-precision. Consider creating handle to your own function that calls the builtin.][1]][1]
variable type information:
I want to fix the warning message. How to cast the second out parameter?
full function code
function [isDuplicate,lineLeft] = check_similar_line(nAngle,nDistance)
%UNTITLED21 delete duplicate line angle < pi/180*4 and distance to
%original<4
% input
% output
% 1、isDuplicate
% 2、lineLeft,index of line left
size = int16(length(nAngle));
angleDifference = abs(triu(bsxfun(#minus,nAngle,nAngle'),-1)) + tril(inf(size,size));
distanceDifference = single(abs(triu(bsxfun(#minus,nDistance,nDistance'),-1)) + tril(inf(size,size)));
angleIndex = int32(find(angleDifference<pi/180*4));
% distanceDifference(index)
distanceIndex = int32(find(distanceDifference(angleIndex) < 4));
finalIndex = int32(angleIndex(distanceIndex));
[left,~] = ind2sub([size,size],finalIndex);
lineLeft = setdiff(1:size,left);
isDuplicate = ~isempty(finalIndex);
end

How can I declare 2-dimentional array as parameter in function In MATLAB?

I have a function that gets a matrix and do some operations on it.
but I do not know how to declare function's parameter .
this is my function Code:
function D=Shirinkage(a)
D(1,:)=a(1,:);%this is X1
for i=2:4
D(i,4)=0;
for j=1:3
D(i,j)=1/2*(a(1,j)+a(i,j));
D(i,4)=D(i,j)^2 + D(i,4); %object function
end
end
end
I tried a(4,4) instead of a (parameter of the function),but the error does not appear.
Error:
??? Input argument "a" is undefined.
Error in ==> Shirinkage at 3
D(1,:)=a(1,:);%this is X1
also I want to declare D correctly.
appreciating any help.
Edited:
i call my function from a script file , in this way:
I have a 2-dimention array(matrix) its size is : 4*4 and its name is A.
i want my function gets this matrix and do the operation on it and the result can be saved again in it.
A=Shirinkage(A)
e.x. A has this values:
A=[1,2,3,4;2,3,4,5;5,6,7,8;1,2,3,4]
The function you created is working fine. The only recommendation I have to pre-allocate the size of D as it varies in each iteration in your current code.
function D = Shirinkage(a)
D = zeros(size(a));
D(1,:) = a(1,:); %this is X1
for i = 2:4
D(i,4) = 0;
for j = 1:3
D(i,j) = 0.5*(a(1,j) + a(i,j));
D(i,4) = D(i,4) + D(i,j)^2; %object function
end
end
end
The function was called from command window by using the same matrix you have used and it gave the following output.
The error you have posted says that the function hasn't received the argument a. If your script and the function are in the same MATLAB path, this should work perfectly.

Object array implemenation in MATLAB OOP

Suppose that we have this class in MATLAB R2014b :
classdef Pre
properties
Value
end
methods
function obj = Pre(F)
if nargin ~= 0
m = size(F,1);
n = size(F,2);
obj(m,n) = Pre;
for i = 1:m
for j = 1:n
obj(i,j).Value = F(i,j);
end
end
end
end
end
end
1) If we erase if nargin~=0 from this code we have this error :
Error using Pre (line 13)
Not enough input arguments.
Error in Pre (line 15)
obj(m,n) = Pre;
Why? I think this is only checking for number of input arguments !
2) what is obj(m,n) = Pre;? what this line is doing in this code? This is for pre-allocating but how this line can do that?
I checked this class with this syntax: az = Pre([2 3 5;5 3 0])
1) In the line obj(m,n) = Pre; you call Pre without any input argument, so the variable F doesn't exist in that function call. It would therefore fail to do size(F,1) etc. MATLAB prevents this by throwing the error not enough input arguments. As long as you want the possibility to call Pre with or without input arguments, you need to check if an argument exists or not.
2) This does a pre-allocation. By creating an empty Pre at the location (m,n) of obj, MATLAB will initialize obj as matrix of type Pre and size mxn. (You can verify this for normal variables by typing a(2,2) = 0 in the MATLAB console - it will return a 2-by-2 matrix of zeros).

First input must be function handle error using arrayfun()

I'm trying to use arrayfun() to map a function over a cell array. The following is happening:
>> arrayfun(solveFunc, equArray)
Error using arrayfun
First input must be a function handle.
Error in solve>genGuess (line 33)
funcVals = abs(arrayfun(inFunc, xValues));
Error in solve (line 8)
x = genGuess(inFunc, varargin{1}, varargin{2});
Error in makeSolveFunc>#(func)solve(func,start,stop) (line 3)
sFunc = #(func) solve(func, start, stop);
But, the first input IS a function handle. Also... if I manually apply the function to each element of the provided cell array, everything works fine:
>> solveFunc(equArray{1})
ans =
4.7335
>> solveFunc(equArray{2})
ans =
4.7356
Does anyone know why this would be happening? I assumed that if I could manually apply the function to each element of my array, and the return type of the function was consistent and one of the allowed types (you can't for example have arrayfun return an array of function handles... I already tried doing that), it should work. Perhaps that is not the only requirement.
Here is some code that generates this error:
solve.m
function solution = solve(inFunc, start, stop)
%SOLVE solve an equation using Newton's Method
x = genGuess(inFunc, start, stop);
for i = 1:100
m = getSlope(inFunc, x);
x = (m*x - inFunc(x))/m;
end
solution = x;
end
function slope = getSlope(inFunc, x)
%SLOPE calculate the slope at a given point
inc = 1e-5;
if x ~= 0
inc = inc * x;
end
slope = (inFunc(x + inc) - inFunc(x - inc))/(2*inc);
end
function guess = genGuess(inFunc, start, stop)
%GENGUESS get an initial guess to the solution
xValues = linspace(start, stop, 101);
funcVals = abs(arrayfun(inFunc, xValues));
[~, minIndex] = min(funcVals);
guess = xValues(minIndex);
end
charEqu.m
function equ = charEqu(a)
%CHAREQU create a KP model characteristic equation with provided p
equ = #(x) x + a;
end
makeSolveFunc.m
function sFunc = makeSolveFunc(start, stop)
%MAKESOLVEFUNC return a function that solves an equation
sFunc = #(func) solve(func, start, stop);
end
test.m
pArray = 1:5;
equArray = cell(1,arrayLen);
for i = 1:5
equArray{i} = charEqu(pArray(i));
end
solveFunc = makeSolveFunc(1.1*pi, 2*pi);
alphaAArray = arrayfun(solveFunc, equArray);
I have narrowed down the error to something in genGuess(). For some reason, in the line funcVals = abs(arrayfun(inFunc, xValues)); the variable inFunc is a 1x1 cell array containing a function handle. I have no idea why that would be the case. I traced this back to the anonymous function call #(func) solve(func, start, stop); in the makeSolveFunc() function. There it is still a 1x1 cell array containing a function handle. I'm not really sure where that cell array is coming from as that function is getting called from arrayfun().
Background information on what I'm trying to do in case someone wants to suggest a better way:
I'm trying to solve equations using Newton's method. I have written a function that can solve an equation given an initial guess range. This function is the solve() function you can see in the first error message. It takes a function, and the guess range and returns a function that I'm calling solveFunc(). solveFunc() takes a function and solves it using the initial guess range previously provided.
Maybe I'm just too used to functional programming and should just use a loop.
If the arguments passed to the function handle are contents of elements of a cell array, you need to use cellfun instead of arrayfun:
cellfun(solveFunc, equArray)
This is equivalent to
for i=1:length(equArray)
out(i) = solveFunc(equArray{i});
end
since solveFunc is already a function handle.
Check where the error comes from. This line causes the error:
funcVals = abs(arrayfun(inFunc, xValues));
The first input argument is a 1x1 cell containing one function handle. This is caused because equArray is a cell, thus use cellfun as Jonas already mentioned:
pArray = 1:5;
equArray = cell(1,arrayLen);
for i = 1:5
equArray{i} = charEqu(pArray(i));
end
solveFunc = makeSolveFunc(1.1*pi, 2*pi);
alphaAArray = cellfun(solveFunc, equArray);

Error with arrayfun

I have the following function:
function x = dataParser(y)
%// Importing list of places
places = textread('UKPlaceNames.txt' ,'%s');
%// Picking two places
place = char(places(y)); %// Converting them to an array of chars
placeInt = place - 'A' + 1;
x = placeInt;
end
In a separate function, I create:
myVector = 1:6
and then call
myVectorB = arrayfun(dataParser, myVector)
However, MATLAB throws an error on:
place = char(places(y));
stating that there are not enough parameters, where
places = textread('test.txt' ,'%s');
I am a bit confused as to where the error is, since the function takes one paramater and I am providing it...
Thanks!
You should do the following in your main script/function:
myVectorB = arrayfun(#dataParser, myVector)
Note the # in front of dataParser: this passes a function handle of dataParser to arrayfun, instead of evaluating the function dataParser() and passing the result to arrayfun.