Input argument "b" is undefined - matlab

i am new in matlab and search everything. I am writing a the function. i could not able to understand why this error is comning :"Input argument "b" is undefined." . shall i intialise b =0 ? whereas it is the parameter coming from input console. my code:
function f = evenorodd( b )
%UNTITLED2 Summary of this function goes here
%zohaib
% Detailed explanation goes here
%f = b;%2;
f = [0 0];
f = rem(b,2);
if f == 0
disp(b+ 'is even')
else
disp(b+ 'is odd')
end
console:
??? Input argument "b" is undefined.
Error in ==> evenorodd at 6
f = rem(b,2);

From what I see, this is what you are trying to do:
function f = evenorodd( b )
f = rem(b,2);
if f == 0
fprintf('%i is even\n', b)
else
fprintf('%i is odd\n', b)
end
=======================
>> evenorodd(2);
2 is even
No need to initialize f as [0,0].
In MATLAB, you cant concatenate a number and string with + operator. Use fprintf.
The above function evenorodd takes one argument (integer) and returns 0 or 1.

Related

local variable T conflicts with a static parameter warining

I'm getting warning that I don't understand.
I first run the following code:
type PDF{T <: Real}
𝛑::Vector{Float64} # K
μs::Matrix{T} # D x K
Σs::Vector{Matrix{T}} # K{D x D}
end
type Q{T <: Real}
w::Union{Float64, Vector{Float64}}
μpair::Union{Vector{T}, Matrix{T}}
Σpair::Union{Matrix{T}, Tuple{Matrix{T}, Matrix{T}} } # K{D x D}
end
type Smod{T <: Real}
H::Matrix{T} # D x D
Σs::Vector{Matrix{T}} # K{D x D}
qs::Vector{Q{T}}
end
type Scale{T <: Real}
μ::Vector{T} # D
Σ::Matrix{T} # D x D
end
type Parameters{T <: Real}
scale::Scale{T}
w::Vector{Float64}
maxNumCompsBeforeCompression::Integer
numComponentsAbsorbed::Integer
end
type KDE{T}
pdf::PDF{T}
smod::Smod{T}
params::Parameters{T}
end
And when after this I run the following snippet in IJulia
function initializeKDE{T <: Real}(x::Vector{T})
d = size(x,1)
T = typeof(x)
𝛑 = ones(Float64, 1)
μs = Array(T, d,1)
μs[:,1] = x
Σs = Array(Matrix{T}, 0)
pdf = PDF(𝛑, μs, Σs)
H = zeros(T, d,d)
qs = Array(Q{T}, 0)
smod = Smod(H, Σs, qs)
scale = Scale(x, H)
w = [0.0, 1.0]
maxNumCompsBeforeCompression = min(10, (0.5d^2 + d))
numComponentsAbsorbed = 0
params = Params(scale, w, maxNumCompsBeforeCompression, numComponentsAbsorbed)
kde = KDE(pdf, smod, params)
return kde::KDE
end
I get the following warning:
WARNING: local variable T conflicts with a static parameter in initializeKDE at In[4]:3.
where In[4]:3 corresponds to the 3rd line of the 2nd snippet.
Can anyone explain in human english what this warning is saying?
This is saying that you are trying to use T in two different ways: once as a "static parameter" and once as a local variable.
Firstly, you are using T as the parameter with which you are parametrising the function initializeKDE:
function initializeKDE{T <: Real}(x::Vector{T})
But then you are trying to redefine a new T in that third line:
T = typeof(x)
What are you trying to do here? If you are trying to define T to be the type of the elements that the vector x contains, then you should just delete this line and everything should just work -- T will automatically take the element type (eltype) of the vector that you pass to the initializeKDE function.

Cython and Scipy

I'm cythonizing a skript that containes scipy.stats.norm() function for calculation of implied vola.
Instead of scipy.stats.norm() I use scipy.special.ndtr() since this is somewhat faster. However, when profiling my script most time (50 from 125sec) is still spent within this function, in particular within _distn_infrastructure.py:1610(cdf).
That's the function:
def cdf(self, x, *args, **kwds):
"""
in class rv_continuous(rv_generic):
Cumulative distribution function of the given RV.
Parameters
----------
x : array_like
quantiles
arg1, arg2, arg3,... : array_like
The shape parameter(s) for the distribution (see docstring of the
instance object for more information)
loc : array_like, optional
location parameter (default=0)
scale : array_like, optional
scale parameter (default=1)
Returns
-------
cdf : ndarray
Cumulative distribution function evaluated at `x`
"""
args, loc, scale = self._parse_args(*args, **kwds)
x, loc, scale = map(asarray, (x, loc, scale))
args = tuple(map(asarray, args))
x = (x-loc)*1.0/scale
cond0 = self._argcheck(*args) & (scale > 0)
cond1 = (scale > 0) & (x > self.a) & (x < self.b)
cond2 = (x >= self.b) & cond0
cond = cond0 & cond1
output = zeros(shape(cond), 'd')
place(output, (1-cond0)+np.isnan(x), self.badvalue)
place(output, cond2, 1.0)
if any(cond): # call only if at least 1 entry
goodargs = argsreduce(cond, *((x,)+args))
place(output, cond, self._cdf(*goodargs))
if output.ndim == 0:
return output[()]
return output
However, I neighter see any function that does the actual cdf calculation nor a call of a another function that actually does it. I tried to print the output of this function via inserting
print output
before
return output
however, the print command is highlighted as wrong syntax and when running the skript there is no print of the output. How to go from here? I somehow need to speed up the norm-CDF calculation.

Error in Matlab, calling a function

The errors are:
Error in getvalues (line 4)
faceNoNoise = wiener2(x, [5 5]);
Output argument "mouthTall" (and maybe others) not assigned during call to "C:\Users\Trent\face\getvalues.m>getvalues".
Error in finalProject2 (line 10)
[ numWhiteEyebrow, mouthTall, eyebrowHeight ] = getvalues( faceGray )
faceNoNoise = wiener2(x, [5 5]); <---- getvalues.m line with error
function finalProject2(x) <----- finalproject file
face = imread(x);
faceGray = rgb2gray(face);
numWhiteEyebrow = 0;
mouthTall = 0;
eyebrowHeight = 0;
[ numWhiteEyebrow, mouthTall, eyebrowHeight ] = getvalues( faceGray ) <--error above
end
Im trying to figure out why its doing this...
This happens since you have not assigned required output values in the function getvalues.m. There are 3 output arguments which should be returned by getvalues.m. See this and this. To clarify even more, if your function definition in getvalues.m file is [a,b,c]=getvalues(x) (and this is how it would mostly be in your case) then in your getvalue.m file, you should have variables a, b and c.

Matlab function calling basic

I'm new to Matlab and now learning the basic grammar.
I've written the file GetBin.m:
function res = GetBin(num_bin, bin, val)
if val >= bin(num_bin - 1)
res = num_bin;
else
for i = (num_bin - 1) : 1
if val < bin(i)
res = i;
end
end
end
and I call it with:
num_bin = 5;
bin = [48.4,96.8,145.2,193.6]; % bin stands for the intermediate borders, so there are 5 bins
fea_val = GetBin(num_bin,bin,fea(1,1)) % fea is a pre-defined 280x4096 matrix
It returns error:
Error in GetBin (line 2)
if val >= bin(num_bin - 1)
Output argument "res" (and maybe others) not assigned during call to
"/Users/mac/Documents/MATLAB/GetBin.m>GetBin".
Could anybody tell me what's wrong here? Thanks.
You need to ensure that every possible path through your code assigns a value to res.
In your case, it looks like that's not the case, because you have a loop:
for i = (num_bins-1) : 1
...
end
That loop will never iterate (so it will never assign a value to res). You need to explicitly specify that it's a decrementing loop:
for i = (num_bins-1) : -1 : 1
...
end
For more info, see the documentation on the colon operator.
for i = (num_bin - 1) : -1 : 1

Input argument "ip" is undefined Error

I am new in matlab I am trying to define a function and I keep getting this error
" input argument "ip" is undefined.
Error in ==> edge_mapping at 2
size_ip = size(ip(:,:,1)); "
here is my code
function[op1,op2,op3] = edge_mapping(ip)
size_ip = size(ip(:,:,1));
s=size_ip(1);
op1= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op2= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op3= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
for i = 1 : 10
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
function [op1, op2, op3] = edge_mapping(ip)
op1 = zeros(size(ip));
op2 = zeros(size(ip));
op3 = zeros(size(ip));
for i = 1 : size(ip, 3)
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
return
But I think it's better to write a simple function:
function op = edge_mapping(ip, edge_mode)
op = zeros(size(ip));
for i = 1 : size(ip, 3)
op(:,:,i)=edge(ip(:,:,i), edge_mode);
end
return
and then call it:
op_sobel = edge_mapping(ip, 'sobel');
op_prewitt = edge_mapping(ip, 'prewitt');
op_canny = edge_mapping(ip, 'canny');
This code is a function. It has to be save as m-file and run from MATLAB command line, script or another function as
[op1,op2,op3] = edge_mapping(ip);
where arguments ip, op1, op2, and op3 can have different names.
Make sure you have space after function keyword.
To avoid this error you can assign a default value for the input argument if it's undefined (not exist in the function's scope):
if ~exist(ip, 'var')
ip = []; %# or other default value
end