MATLAB - Directly convert nominal data to numeric - matlab

Is there any Matlab function to directly convert nominal data to numeric one ?
N.b that currently, and after several searches, I use NumericGroup=str2num(char(NominalGroup))
Thanks,

double(NominalGroup) will convert a nominal/categorical array to a double array.

Using the double function is dangerous because it returns the category index of the NominalValue.
Example:
color = categorical({'8','9','0'})
x = color(1) % x = 8
z = double(x) % z = 2
I have been loking for a function that gives me z = 8.

Related

Understanding Non-homogeneous Poisson Process Matlab code

I have found the following Matlab code to simulate a Non-homogeneous Poisson Process
function x = nonhomopp(intens,T)
% example of generating a
% nonhomogeneousl poisson process on [0,T] with intensity function intens
x = 0:.1:T;
m = eval([intens 'x']);
m2 = max(m); % generate homogeneouos poisson process
u = rand(1,ceil(1.5*T*m2));
y = cumsum(-(1/m2)*log(u)); %points of homogeneous pp
y = y(y<T); n=length(y); % select those points less than T
m = eval([intens 'y']); % evaluates intensity function
y = y(rand(1,n)<m/m2); % filter out some points
hist(y,10)
% then run
% t = 7 + nonhomopp('100-10*',5)
I am new to Matlab and having trouble understanding how this works. I have read the Mathworks pages on all of these functions and am confused in four places:
1) Why is the function defined as x and then the intervals also called x? Like is this an abuse of notation?
2) How does the square brackets affect eval,
eval([intens 'x'])
and why is x in single quotations?
3) Why do they use cumsum instead of sum?
4) The given intensity function is \lambda (t) = 100 - 10*(t-7) with 7 \leq t \leq 12 How does t = 7 + nonhomopp('100-10*',5) represent this?
Sorry if this is so much, thank you!
To answer 2). That's a unnecessary complicated piece of code. To understand it, evaluate only the squared brackets and it's content. It results in the string 100-10*x which is then evaluated. Here is a version without eval, using an anonymous function instead. This is how it should have been implemented.
function x = nonhomopp(intens,T)
% example of generating a
% nonhomogeneousl poisson process on [0,T] with intensity function intens
x = 0:.1:T;
m = intens(x);
m2 = max(m); % generate homogeneouos poisson process
u = rand(1,ceil(1.5*T*m2));
y = cumsum(-(1/m2)*log(u)); %points of homogeneous pp
y = y(y<T); n=length(y); % select those points less than T
m = intens(y); % evaluates intensity function
y = y(rand(1,n)<m/m2); % filter out some points
hist(y,10)
Which can be called like this
t = 7 + honhomopp(#(x)(100-10*x),5)
the function is not defined as x: x is just the output variable. In Matlab functions are declared as function [output variable(s)] = <function name>(input variables). If the function has only one output, the square brackets can be omitted (like in your case). The brackets around the input arguments are, as instead, mandatory, no matter how many input arguments there are. It is also good practice to end the body of a function with end, just like you do with loops and if/else.
eval works with a string as input and the square brackets apprently are concatenating the string 'intens' with the string 'x'. x is in quotes because, again, eval works with input in string format even if it's referring to variables.
cumsum and sum act differently. sum returns a scalar that is the sum of all the elements of the array whereas cumsum returns another array which contains the cumulative sum. If our array is [1:5], sum([1:5]) will return 15 because it's 1+2+3+4+5. As instead cumsum([1:5]) will return [1 3 6 10 15], where every element of the output array is the sum of the previous elements (itself included) from the input array.
what the command t = 7 + nonhomopp('100-10*',5) returns is simply the value of time t and not the value of lambda, indeed by looking at t the minimum value is 7 and the maximum value is 12. The Poisson distribution itself is returned via the histogram.

Using the 'solve' function

I would like to solve an equation for x, and i know that there are atleast two solutions,which means that jj will be a vector. I need the largest of those solutions - that is were max(jj) comes into play. However z = max(jj)
will give me the biggest number, but it does not evaluate it. for example z could be = 2*3^4 + 1 . In this form i can't send this "number" to another function which I want to do. the 'k' is a given number not a variable. (say k=10 or any other number)
syms x
eqn = x + (k/6)*(x^2 -1) ==0
jj = solve(eqn,x)
z = max(jj)
You are looking for a way to convert from symbolic to numeric form. There is a standard symbolic toolbox function for that: double.
z1=double(z)
should return the value of the expression in a double format array.

integral2 with fun calculated using vector

Im new to MATLAB. Want to use integral2 as follows
function num = numer(x)
fun=#(p,w) prod((p+1-p).*(1-w).*exp(w.*x.*x/2))
num= integral2(fun ,0,1,0,1)
end
I get several errors starting with
Error using .*
Matrix dimensions must agree.
Error in numer>#(p,w)prod(p+(1-w).*exp(w.*x.*x/2)) (line 5)
fun=#(p,w) prod(p+(1-w).*exp(w.*x.*x/2))
Can you please tell me what I do wrong.
Thanks
From the help for integral2:
All input functions must accept arrays as input and operate
elementwise. The function Z = FUN(X,Y) must accept arrays X and Y of
the same size and return an array of corresponding values.
When x was non-scalar, your function fun did not do this. By wrapping everything in prod, the function always returned a scalar. Assuming that your prod is in the right place to begin with and taking advantage of the properties of the exponential, I believe this version will do what you need for vector x:
x = [0 1];
lx = length(x);
fun = #(p,w)(p+1-p).^lx.*(1-w).^lx.*exp(w).^sum(x.*x/2);
num = integral2(fun,0,1,0,1)
Alternatively, fun = #(p,w)(p+1-p).^lx.*(1-w).^lx.*exp(sum(x.*x/2)).^w; could be used.

how to Convert Matlab fft function to cvDft in opencv

I'm Very new to opencv.I need to convert the code from matlab to opencv.i have problem with use fft in matlab.i have a one dimensional matrix a.and i'm going apply fft in that as given below.
a = [0;0;0;0;0;0;0;0;0.09707;0.0998;0.1202;-0.1606;-0.0913;0.1523;0.1288];
c = abs(fft(a,15));
c >> 0.3463
0.1056
0.3608
0.5705
0.4232
0.2407
0.1486
0.1488
0.1488
0.1486
0.2407
0.4232
0.5705
0.3608
0.1056
C is my result,which i got from matlab.while i'm going to use cvDFT for this one the results are differnt.please help me with some example..my c code is given below...
CvMat* fftin = cvCreateMat(nn,1,CV_64FC2);
CvMat* fftout = cvCreateMat(nn,1,CV_64FC2);
cvZero(b); ////consider a hav the value of mat b is empty for imgin
cvMerge(a,b,NULL,NULL,fftin);
cvDFT(fftin,fftout,CV_DXT_FORWARD,0);
cvSplit(fftout,out_real,out_img,0,0);
for (int i = 0;i<out_real->rows;i++)
{
double val11= cvGetReal2D(out_real,i,0);
double val12= cvGetReal2D(out_img,i,0);
val11 = abs(val11);
val12 = abs(val12);
printf("DFT value is:%f %f\n",val11,val12);
cvSetReal2D(C_out,i,0,val11);
}
In your first example, you seem to be printing the magnitude of each complex value in the result. But in your second example you seem to be printing the absolute value of each component of the complex values (e.g. X and Y instead of the length or hypotenuse).

"Index Exceeds Matrix Dimensions" neural network function error

I've got two datasets, which I load from a CSV file, and split them into X and T:
X (3x5000) double
T (1x5000) double
I'm trying to configure this function, but I can't
http://www.mathworks.co.uk/help/toolbox/nnet/ref/layrecnet.html
X has three features and 5000 examples. T has one feature and 5000 examples. For an example the target is feature 1 20 steps ahead. So basically X(1,21) == T(1).
[X,T] = simpleseries_dataset;
This works perfectly, in this case, I have 1x100, 1x100.
If I use my own data set, however, I get this:
X = data(:,1:3)';
T = data(:,4)';
net = layrecnet(1:2,10);
[Xs,Xi,Ai,Ts] = preparets(net,X,T);
??? Index exceeds matrix dimensions.
Error in ==> preparets at 273
ti = tt(:,FBS+((1-net.numLayerDelays):0));
I don't understand, what am I doing wrong?
UPDATE
I've noticed that my data set is T (1x5000) double while the example dataset is T (1x100) cell. What's the difference between double and cell?
I solved it by:
X = num2cell(X);
T = num2cell(T);
I have no idea why; it must be MATLAB syntax...
You can solve it by:
P = con2seq(p);
T = con2seq(t);
.....% for example
p=(1 2;3 4;5 6);
t=(3;7;11);
.....%now
P = con2seq(p);
T = con2seq(t);
net = elmannet(1:2,12);
[Xs,Xi,Ai,Ts] = preparets(net,P,T);
net = train(net,Xs,Ts,Xi,Ai);
view(net)
Y = net(Xs,Xi,Ai);
perf = perform(net,Ts,Y);
To clarify "(...)it must be MATLAB syntax...":
The problem here is the conversion from double to cell arrays. Matlab does not do this automatically since a cell can contain any type of value as mentioned here: http://www.mathworks.com/help/matlab/matlab_prog/what-is-a-cell-array.html
So, as mentioned in your answer, you can either convert your double arrays to cell arrays using num2cell() or you can allocate X and T as cell arrays from the very beginning using cell() and then copying your double values into them. This explicit type cast is necessary because preparets expects cell arrays as input, much like many of the plot functions in the ANN package.