How to see for which values a matrix loses rank? - matlab

I'm using Matlab and I have the following matrix:
where c2 mens cos(q2), c23 means cos(q2+q3), c234 means cos(q2+q3+q4) and analogously: s2 means sin(q2), s23 means sin(q2+q3), s234 means sin(q2+q3+q4)
Is there a way to see for which values of q2, q3 and q4 the matrix loses rank?

Related

What is a Mat-lab function for « If-in »?

Problem statement: Provide a function that does the following: c1) two vectors D1 and D2 have 7 elements each, then form the division between the corresponding components of each vector and assign the result to a vector name D3, placing a statement that avoids division by zero (i.e., does not divide if element to denominator is null);
The idea of the problem, is to set an error message whenever one of the elements of vector D2 is equal to 0.
My attempt:
D1 = [d1 d2 d3 d4 d5 d6 d7]
D2= [d21 d22 d23 d24 d25 d26 d27]
for i= 1:length(D1)
if 0 in D2
fprintf(‘error: division by 0/n’)
else
D3=D1./D2
end
I don’t know if the “if-in” structure exists in Matlab. If it doesn’t, what could be an equivalent?
Thanks in advance!!!
One way to avoid any division by zero is to modify D2 by replacing any 0 with nan. Divisions by nan produce nan, so it's easy to tell which division would have caused a problem by simply inspecting the resulting vector D3. Moreover, almost all Matlab's functions are able to handle nans nicely (i.e. without crashing) or can be instructed to do so by setting some option.
What I've just described can be accomplished by using logical indexing, as follows:
% Definition of D1 and D2
D1 = [d1 d2 d3 d4 d5 d6 d7]
D2 = [d21 d22 d23 d24 d25 d26 d27]
% Replace 0s with NaNs
D2(D2==0) = nan;
% Perform the divisions at once
D3 = D1./D2 ;
For more details on logical indexing, look at the relevant section here.
As the OP requests a function that does the job, here's a possible implementation:
function D3 = vector_divide(D1, D2)
% Verify that vectors are numeric
% and have the same dimensions
if isnumeric(D1) & isnumeric(D2) &...
(size(D1,1) == size(D2,1)) &...
(size(D1,2) == size(D2,2))
% replace 0s with NaNs
D2(D2==0) = nan;
% Perform the divisions at once
D3 = D1./D2 ;
else
disp('D1 and D2 should both be numeric and have the same size!');
D3 = [];
end
Error handling in case of non-numeric arrays or size mismatch might vary depending on project requirements, if any. For instance, I could have used error (instead of disp) to display a message and terminate the program.

CART algorithm of matlab 'fitctree' takes account on the attributes order why ?

here is an example mentionning that fitctree of matlab takes into account the features order ! why ?
load ionosphere % Contains X and Y variables
Mdl = fitctree(X,Y)
view(Mdl,'mode','graph');
X1=fliplr(X);
Mdl1 = fitctree(X1,Y)
view(Mdl1,'mode','graph');
Not the same model, thus not the same classification accuracy despite dealing with the same features ?
In your example, Xcontains 34 predictors. The predictors contain no names and fitctreejust refers to them by their column numbers x1, x2, ..., x34. If you flip the table, the column number changes and therefore their name. So x1 -> x34. x2 -> x33, etc..
In for most nodes this does not matter because CART always divides a node by the predictor that maximises the impurity gain between the two child nodes. But sometimes there are multiple predictors which result in the same impurity gain. Then it just picks the one with the lowest column number. And since the column number changed by reordering the predictors, you end up with a different predictor at that node.
E.g. let's look at the marked split:
Original order (mdl):
Flipped order (mdl1):
Up to this point always the same predictor and values have been chosen. Names changed due to order, e.g. x5 in the old data = x30 in the new model. But x3 and x6 are actually different predictors. x6 in the flipped order is x29 in the original order.
A scatter plot between those predictors shows how this could happen:
Where blue and cyan lines mark the splits performed by mdl and mdl1 respectively at that node. As we can see, both splits yield child nodes with the same number of elements per label! Therefore CART can chose any of the two predictors, it will cause the same impurity gain.
In that case it seems to just pick the one with the lower column number. In the non-flipped table x3 is chosen instead of x29 because 3 < 29. But if you flip the tables, x3 becomes x32 and x29 becomes x6. Since 6 < 32 you now end up with x6, the original x29.
Ultimately this does not matter - the decision tree of the flipped table is not better or worse. It only happens in the lower nodes where the tree starts to overfit. So you really don't have to care about it.
Appendix:
Code for scatter plot generation:
load ionosphere % Contains X and Y variables
Mdl = fitctree(X,Y);
view(Mdl,'mode','graph');
X1=fliplr(X);
Mdl1 = fitctree(X1,Y);
view(Mdl1,'mode','graph');
idx = (X(:,5)>=0.23154 & X(:,27)>=0.999945 & X(:,1)>=0.5);
remainder = X(idx,:);
labels = cell2mat(Y(idx,:));
gscatter(remainder(:,3), remainder(:,(35-6)), labels,'rgb','osd');
limits = [-1.5 1.5];
xlim(limits)
ylim(limits)
xlabel('predictor 3')
ylabel('predictor 29')
hold on
plot([0.73 0.73], limits, '-b')
plot(limits, [0.693 0.693], '-c')
legend({'b' 'g'})

A set of results in Decision Tree

I have a data which is 1672x6. I have put some of them in the picture.
where x values are A1 A2 A3 A4 A5 A6 and y values are B1 B2 ....B1672.
I used the following code while generating decision trees:
vars = {'A1', ' A2 ','A3',' A4 ','A5',' A6'}
x = [A1 A2 A3 A4 A5 A6];
y = [B];
t = classregtree(x, y, 'method','classification', 'names',vars, ...
'categorical',[2 4], 'prune','off');
view(t)
and it generates super crazy trees like
I want to get the values which are greater than the values that I gave. When I say :
inst = [3 2.3 2 0 1 0];
prediction = eval(t, inst)
It only gives me the B value (like B271) which has that variable but I want to get all B variables which have greater values than inst variable such as A1>3 A2>2.3 A3>2 A4>0 A5>1 A6>0. How can I get them?
You seem to confuse two things: decision tree and finding desired rows
If you want to find all the rows that are greater than inst, the following is a simple code that prints all such rows.
for i = 1:size(B,1)
if all(a(i,:)>inst)==1
i,
end
end
However, decision tree is a totally different topic. In a decision tree, you have a set of conditions (A1 to A6 in your case) and many rows for training (B1 to B1672) and a consequence for each one of them. When a new test case is queried, the machine decides on best possible consequence out of all the consequences.
Some decision tree totorials: 1, 2 and wikipedia

matlab functions about sine curve

I have a question about matlab programming about sine curve.
The question is as below:
Consider the definition: function [s1, s2, sums] = sines(pts,amp,f1,f2). The input, pts, is an integer, but amp, f1, and f2 and are not necessarily integers. Output argument s1 is a row vector whose length (number of elements) equals pts. The elements of s1 are the values of the sine function when it is given equally spaced arguments that start at zero and extend through f1 periods of the sine. (Note that we ask for full periods, so if f1 is an integer, both the first and the last element of s1 will be 0 other than a very small rounding error.) The amplitude of the sine wave equals amp. The vector s2 is the same as s1 except that s2 contains f2 periods. The vector sums is the sum of s1 and s2. If f2 is omitted, then it should be set to a value that is 5% greater than f1. If f1 is omitted also, then it should be set to 100. If amp is not provided, then it should default to 1. Finally, if pts is omitted as well, then it should be set to 1000.
Here is what I am confused: how to define step length pts. I used the following method but it fails to work. Please help me to fix it.
function [s1, s2, sums] = sines(pts,amp,f1,f2)
.................
t = linspace(0, 1, pts);
s1=amp*sin(2*pi*f1*t);
s2=amp*sin(2*pi*f2*t);
Thanks.
As far as the part of the code you are confused this should work for you:
n=pts-1
t=0:n;
s1=amp*sin(2*pi*f1/n*t);
s2=amp*sin(2*pi*f2/n*t);
then you sum s1+s2. You still need to handle the missing input if any.

Conceptual issues in AR model

I have some basic questions regarding multivariate model. In the ARFIT toolbox, the demo file ardem.m shows the working of a 2nd order bivariate (v1,v2) AR model. The coefficient matrices
A1 = [ 0.4 1.2; 0.3 0.7 ]
A2 = [ 0.35 -0.3; -0.4 -0.5 ]
are concatenated into
A = [ A1 A2 ]
Then a transpose of A is taken. So the result is a 2*4 matrix.
My question is that there should be only 4 coefficients viz. 2 for v1 variable and 2 for v2 variable but why are there 8 coefficients? If the equation format is
v(k,:) = a11*v1(k-1)+a12*v1(k-2) + a21*v2(k-1)+ a22*v2(k-2)
where a11 = 0.4, a12=1.2, a21=0.3 and a22=0.7.
I think I am missing somewhere in understanding. Can somebody please explain what is the correct representation?
The matrices A1 and A2 contain transfer coefficients that describe the contribution of states at times k-1 and k-2, respectively, to the state at time k. Since this is a bivariate process, we are following two variables which can influence each other, and both A1 and A2 are 2 x 2. Writing v1 = v(k,1) and v2 = v(k,2):
v1(k) = A1(1,1)*v1(k-1) + A1(1,2)*v2(k-1) + A2(1,1)*v1(k-2) + A2(1,2)*v2(k-2)
and similarly for v2(k). Then collectively A1 and A2 contain 8 elements. If the two processes were independent then A1 and A2 would be diagonal and would collectively contain only 4 nonzero elements.
By the way this is not really a Matlab question so I don't think this is the right forum for this question.