errors using colfilt to speed up computation - matlab

I'm trying to perform a sliding neighborhood operation using colfilt.
I am basically trying to run a 2X2 window on an image while running some functions:
f = #(x,y) (func(diff(x:x+1,y:y+1),s(x:x+1,y:y+1)));
e = colfilt(img, [2 2], 'silding', f);
where diff and s are same size as the img (responses calculated in various ways).
func is a function that performs some computations on diff and s.
I keep getting this error:
Matrix dimensions must agree.
Error in colfilt (line 133)
if all(block>=size(a)), % Process the whole matrix at once.
Error in create_e (line 14)
e = colfilt(img, [2 2], 'silding', f);
any thoughts would be appreciated.
Thanks a lot.

I think you are wanting to use a different function, nlfilter, or you need to adapt your function, because it seems you're assuming the input of your function is a 2-by-2 array.
colfilt reshapes each block into a nElementsInBlock-by-1 array and arranges them in a nElementsInBlock-by-nBlocks array, so that you can calculate the function (e.g. kurtosis) in a single step.
nlfilter applies a function on a sliding window, passing each block as a n-by-m array to a user-defined function.

Related

Simulink: Syntax error

I have a syntax error in the following equation:
((Cs+Cf)*u(1)/Cf-Cs*u(2)/Cf)*(1/(1+(Cs+Cf)/(Cf*(10^(u0/20)))))*(1-exp(-(pi*f1*(10^9)/(fs*10^6))*(Cf/(Cs+Cf)+1/(10^(u0/20)))))
Uppercase/lowercase checked, they are ok. What could be the problem?
As mentioned in the documentation of Interpreted MATLAB Function,
The Interpreted MATLAB Function block accepts one real or complex
input of type double ...
Here you're using multiple inputs which are Cs, Cf, u0, u, f1 and fs.
How to solve it?
Solution-1: Using Interpreted MATLAB Function block
One way to deal with this problem would be to concatenate all the input matrices into a single matrix and use its indices to represent each value in the equation.
e.g; if you have:
u=[1 5]; u0=5; Cs=1; Cf=1; f1=1; fs=20;
Concatenate them into a single matrix in your workspace. Something like the following would do:
new=[u, u0, Cs, Cf, f1, fs];
%It could be different depending on the dimensions of these
%variables that you actually have
then use the following equation according to the indices of new in the Interpreted MATLAB Function block:
((new(4)+new(5))*u(1)/new(5)-new(4)*u(2)/new(5))*(1/(1+(new(4)+new(5))/(new(5)*(10^(new(3)/20)))))*(1-exp(-(pi*new(6)*(10^9)/(new(7)*10^6))*(new(5)/(new(4)+new(5))+1/(10^(new(3)/20)))))
Solution-2: Using MATLAB Function block
You can also use the MATLAB Function block in which you can use multiple inputs. For your case, write the following code in it:
function y = foo(u,u0,Cs,Cf,f1,fs)
y = ((Cs+Cf)*u(1)/Cf-Cs*u(2)/Cf)*(1/(1+(Cs+Cf)/(Cf*(10^(u0/20)))))* ...
(1-exp(-(pi*f1*(10^9)/(fs*10^6))*(Cf/(Cs+Cf)+1/(10^(u0/20)))));
and connect Constant blocks with its inputs and give the values of the constants equal to the respective variables that you want to use.

nlinfit Dimension Error

I am using nonlinearfit tool in matlab.
I keep getting the following error:
Error using nlinfit (line 210)
MODELFUN must be a function that returns a vector of fitted values the same size as Y (1-by-100). The model function you provided returned a result that was 1-by-2.
One common reason for a size mismatch is using matrix operators (, /, ^) in your function instead of the corresponding element-wise operators (., ./, .^).
I found this question very similar to mine, but still I get the same error. I have tried calculating myfun on the console while using a vector as an input, which gives me output of correct dimension. It will be ton of help if anybody can point out the mistake.
% Defining the function
myfun = #(t,b)exp(t.*b(1)+b(2));
[y_a] = arrayfun(myfun,x_a);
% Using nonlinear least square minimization
beta0 = [1 1];
nlinfit(x,y,myfun, beta0)
Thanks in advance...:)
Edit: Found this to be working.
g = fittype('exp(k*x + a)');
[fit1,gof,fitinfo] = fit(x',y',g,'StartPoint',[1 1]);
The function used in nlinfit takes the parameter vector as its first argument, then the independent data vector. You want,
myfun = #(b,t)exp(t.*b(1)+b(2));
Note that you could just use * rather than .* in this instance too.

not enough input arguments fminsearch

I'm trying to write a script in MATLAB that graphs a function in three dimensions using the mesh function and then finds the maximum of the surface. This is my code so far:
%% Aquifer, 3D maximum search
figure(2)
[X,Y] = meshgrid(-10:.5:10,-10:.5:10);
h = #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y));
mesh(h(X,Y)) %graph aquifer surface
aquamax = fminsearch(h,[-5;-5])
When I run the code I get this error:
Error using #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y))
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
I've read up on the fminsearch function but I'm not that familiar with it (still a bit of a noob at Matlab). Do I need to rework the code or is it just how I've input things into fminsearch?
Your h function requires 2 scalar inputs, but fminsearch only does one input, possibly a vector. Change h to h = #(x)-(1./(1+(x(1)-.25).^2+(x(2)-.5).^2+x(1)+x(1).*x(2))); and see if that works.

fminsearch multiple parameters matlab

I'm trying to use fminsearch with multiple parameters but I can't seem to even get it working with two. I've also tried using optimization tool in matlab but then I get:
Optimization running.
Error running optimization.
Not enough input arguments.
What i do:
fval = fminsearch(#g,[1 1])
The function g looks like this:
function r=g(x,y)
r=x.^3+3*x*y.^2+12*x*y;
end
but i get this:
Error using g (line 2)
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
Your function g takes two inputs, x and y, however you supply fminsearch one input, the vector [1 1]. You need to rewrite it so that fminsearch only needs a single vector as input, but then that vector is split into two numbers to input into g.
fminsearch(#(v) g(v(1),v(2)),[1 1])
This makes an anonymous function that takes a vector as input (v) and then uses the first element (v(1)) as the first input to g, and the second element as the second input.

How can I write this function in MatLab? I got a Matrix Error

How can I write this function in MatLab? I got a Matrix Error when typing the following in:
n=1:100; k=0.5; x(n)=sin((3*pi*n)/20); y(n)=x(n/k);scale (x,k)
You are evaluating the functions to a vector, further processing these data points is sometimes very tricky. Try function handles instead:
x=#(n)sin((3.*pi.*n)./20)
y=#(n)x(n./k)
Please note, that I replaced the matrix multiplication with element-wise operations, which allows processing of vectors.
To evaluate a function handle to a vector, use y(n) or x(n)