G and GHAT need to be same classification tree - matlab

Can someone help to solve and if possible explain my mistake.
I have a two numeric matrices for using it in classification tree
x: data matrix <2422x39 double>
y: column vector, class label for each instance <2422x1 double>
I'm doing:
t = classregtree(x, y, 'method','classification');
yPredicted = eval(t, x);
cm = confusionmat(y,yPredicted); // error
Error using ==> confusionmat at 52
G and GHAT need to be the same type.
Tree succesfully built. But I cannot get a confusion matrix for that example
I have read that post to write above code Decision Tree in Matlab
If I use exactly same example from link, its working but when I use my own its not working.
Same steps I took for building regression tree ( t = classregtree(x, y) ) and no error in confusionmat() function.
Please explain what I'm doing incorrectly.
Thanks in advance

It seems to me in your case, eval(t,x) returns cells of char type, while your x and y come with "double" type instead of "char".
The reason the code in Decision Tree in Matlab works is because:
y = strcat(Origin,{});
returns y that is a cell with "char". Thus the argument G and GHAT have the same type.
So, select one that suits your problem:
Approach A: convert yPredicted to numeric matrix
Edit this line :
yPredicted = eval(t, x);
to :
yPredicted = str2num( cell2mat( eval(t, x) ) );
Approach B: convert y to cell of char before calling confusionmat()
y = num2cell( num2str(y) )

Related

How do functions really work in MATLAB?

function [dhdt, x] = velocity(t, h)
dhdt = -9.8 * t;
x = 4 * t;
end
So this is basically my function (with filename velocity.m). At first I thought that what's between the brackets [] would be the output. When I typed in the Command Window I only got one answer.
velocity(1)
%// -9.8
I expected to get a two-element vector containing both dhdt and x
velocity(1)
%// -9.8 4
Why is this?
Matlab only displays one output if you don't store them to variables... Type [dhdt, x] = velocity(1) and you'll see both values, as well as having them stored to variables.
Also, you only get away in this case with not providing the h parameter because it's not used in the function. If you used h in velocity() and called velocity(1) it would break.

Numerical integration of symbolic differentiation - MATLAB

The following is a MATLAB problem.
Suppose I define an function f(x,y).
I want to calculate the partial derivative of f with respect to y, evaluated at a specific value of y, e.g., y=6. Finally, I want to integrate this new function (which is only a function of x) over a range of x.
As an example, this is what I have tried
syms x y;
f = #(x, y) x.*y.^2;
Df = subs(diff(f,y),y,2);
Int = integral(Df , 0 , 1),
but I get the following error.
Error using integral (line 82)
First input argument must be a function
handle.
Can anyone help me in writing this code?
To solve the problem, matlabFunction was required. The solution looks like this:
syms x y
f = #(x, y) x.*y.^2;
Df = matlabFunction(subs(diff(f,y),y,2));
Int = integral(Df , 0 , 1);
Keeping it all symbolic, using sym/int:
syms x y;
f = #(x, y) x.*y.^2;
Df = diff(f,y);
s = int(Df,x,0,1)
which returns y. You can substitute 2 in for y here or earlier as you did in your question. Not that this will give you an exact answer in this case with no floating-point error, as opposed to integral which calculated the integral numerically.
When Googling for functions in Matlab, make sure to pay attention what toolbox they are in and what classes (datatypes) they support for their arguments. In some cases there are overloaded versions with the same name, but in others, you may need to look around for a different method (or devise your own).

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 use multiple parameters with fittype in Matlab

I have a 1000x2 data file that I'm using for this problem.
I am supposed to fit the data with Acos(wt + phi). t is time, which is the first column in the data file, i.e. the independent variable. I need to find the fit parameters (A, f, and phi) and their uncertainties.
My code is as follows:
%load initial data file
data = load('hw_fit_cos_problem.dat');
t = data(:,1); %1st column is t (time)
x = t;
y = data(:,2); %2nd column is y (signal strength)
%define fitting function
f = fittype('A*cos(w*x + p)','coefficients','A','problem',{'w','p'});
% check fit parameters
coeffs = coeffnames(f);
%fit data
[A] = fit(x,y,f)
disp('confidence interval/errorbars');
ci = confint(A)
which yields 4 different error messages that I don't understand.
Error Messages:
Error using fit>iAssertNumProblemParameters (line 1113)
Missing problem parameters. Specify the values as a cell array with one element for each problem parameter
in the fittype.
Error in fit>iFit (line 198)
iAssertNumProblemParameters( probparams, probnames( model ) );
Error in fit (line 109)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in problem2 (line 14)
[A] = fit(x,y,f)
The line of code
f = fittype('A*cos(w*x + p)','coefficients','A','problem',{'w','p'});
specifies A as a "coefficient" in the model, and the values w and p as "problem" parameters.
Thus, the fitting toolbox expects that you will provide some more information about w and p, and then it will vary A. When no further information about w and p was provided to the fitting tool, that resulted in an error.
I am not sure of the goal of this project, or why w and p were designated as problem parameters. However, one simple solution is to allow the toolbox to treat A, w, and p as "coefficients", as follows:
f = fittype('A*cos(w*x + p)','coefficients', {'A', 'w', 'p'});
In this case, the code will not throw an error, and will return 95% confidence intervals on A, w, and p.
I hope that helps.
The straightforward answer to your question is that the error "Missing problem parameters" is generated because you have identified w and p as problem-specific fixed parameters,
but you have not told the fit function what these fixed values are.
You can do this by changing the line
[A] = fit(x,y,f)
to
[A]=fit(x,y,f,'problem',{100,0.1})
which supplies the values w=100 and p=0.1 in the fit. This should resolve the errors you specified (all 4 error messages result from this error)
In general specifying some of the quantities in your fit equation as problem-specific fixed parameters might be a valid thing to do - for example if you have determined them independently and have good reason to believe the values you obtained to be reliable. In this case, you might know the frequency w in this way, but you most probably won't know the phase p, so that should be a fit coefficient.
Hope that helps.

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).