[eigvec eigval] = eigs(K_center,[],neigs,'lm',opts);
disp('***********Eigen value(1)***************')
disp(eigval)
eig_val = eigval ~= 0;
disp('***********Eigen value(2)***************')
disp(eig_val)
Output:
***********Eigen value(1)***************
4.0001 0 0
0 1.0000 0
0 0 1.0000
***********Eigen value(2)***************
1 0 0
0 1 0
0 0 1
The eigen values returned are shown in Eigen value(1)...but after setting eigenvalues ~= 0 ...I get an identity matrix as shown in eigen value(2)..How has it changed to be an identity matrix and why ? I am refering
http://www.mathworks.com/matlabcentral/fileexchange/27319-kernel- pca/content/kernelpca_tutorial.m
Any help appreciated..Thanx in advance..
Related
I need to create all possible permutation matrices for a matrix where every permutation matrix contains only one 1 in each column and each row, and 0 in all other places.
For example, below example in (1) is all possible permutation matrices for 2x2 matrix and in (2) is a all possible permutation matrices for 3x3 matrix and so on
So how can I get these matrices of a matrix NxN in MATLAB and store them into one three-dimensional matrix?
Here's my solution, using implicit expansion (tested with Octave 5.2.0 and MATLAB Online):
n = 3;
% Get all permutations of length n
p = perms(1:n);
% Number of permutations
n_p = size(p, 1);
% Set up indices, where to set elements to 1
p = p + (0:n:n^2-1) + (0:n^2:n^2*n_p-1).';
% Set up indices, where to set elements to 1 (for MATLAB R2016a and before)
%p = bsxfun(#plus, bsxfun(#plus, p, (0:n:n^2-1)), (0:n^2:n^2*n_p-1).');
% Initialize 3-dimensional matrix
a = zeros(n, n, n_p);
% Set proper elements to 1
a(p) = 1
The output for n = 3:
a =
ans(:,:,1) =
0 0 1
0 1 0
1 0 0
ans(:,:,2) =
0 1 0
0 0 1
1 0 0
ans(:,:,3) =
0 0 1
1 0 0
0 1 0
ans(:,:,4) =
0 1 0
1 0 0
0 0 1
ans(:,:,5) =
1 0 0
0 0 1
0 1 0
ans(:,:,6) =
1 0 0
0 1 0
0 0 1
Using repelem, perms and reshape:
n = 3; % matrix size
f = factorial(n); % number of permutation
rep = repelem(eye(n),1,1,f) % repeat n! time the diagonal matrix
res = reshape(rep(:,perms(1:n).'),n,n,f) % indexing and reshaping
Where res is:
res =
ans(:,:,1) =
0 0 1
0 1 0
1 0 0
ans(:,:,2) =
0 1 0
0 0 1
1 0 0
ans(:,:,3) =
0 0 1
1 0 0
0 1 0
ans(:,:,4) =
0 1 0
1 0 0
0 0 1
ans(:,:,5) =
1 0 0
0 0 1
0 1 0
ans(:,:,6) =
1 0 0
0 1 0
0 0 1
And according to your comment:
What I need to do is to multiply a matrix i.e Z with all possible
permutation matrices and choose that permutation matrix which
resulting a tr(Y) minimum; where Y is the results of multiplication of
Z with the permutation matrix. I Think I don't need to generate all
permutation matrices and store them in such variable, I can generate
them one by one and get the result of multiplication. Is that possible
?
You're trying to solve the assignment problem, you can use the well known hungarian algorithm to solve this task in polynomial time. No needs to generate a googleplex of permutation matrix.
I have a logical 1-by-n vector with sum m. Now, I need to convert it into a matrix m-by-n in a way that the row sum is equal 1.
vector (1-by-8) with sum 4
[0 1 0 0 1 0 1 1]
matrix (4-by-8) with row sum 1
[0 1 0 0 0 0 0 0;
0 0 0 0 1 0 0 0;
0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 1]
Is there a mathematically efficient way without calculating the sum, creating a empty matrix, loop through the vector and adding the 1s row by row?
I think that in that case, given your input, you don't even need to calculate the sum.
You can define an identity matrix of size n, then use your input vector to sample the required rows out of it:
I = eye(n);
y = I(x, :) ; % Output Matrix. x is the input vector
Here's another method, using sparse:
matrix = full(sparse(1:m, find(vector), 1, m, n));
I am trying to construct a particular matrix A which multiplies a column vector v= [p_0,0; p_0,1; ... p_0,N; p_1,0; ...; p_N,N].
I know the required matrix (B say) if the vector v was rearranged in the order given by sorting by the second index before the first ( i.e if v were [p_0,0; p_1,0; ... p_N,0; p_0,1; ...; p_N,N]), however would like to rearrange the rows and columns of this matrix to obtain A so that it multiplies the correctly ordered v.
Example:
B =
-2.6667 1.0000 0 0 0 0 0 0 0
-0.5000 0 0.5000 0 0 0 0 0 0
0 -1.0000 -0.4706 0 0 0 0 0 0
0 0 0 -2.6667 1.0000 0 0 0 0
0 0 0 -0.5000 0 0.5000 0 0 0
0 0 0 0 -1.0000 -0.4706 0 0 0
0 0 0 0 0 0 -2.6667 1.0000 0
0 0 0 0 0 0 -0.5000 0 0.5000
0 0 0 0 0 0 0 -1.0000 -0.4706
multiplying v in the wrong order
p_0,0
p_1,0
p_2,0
p_0,1
p_1,1
p_2,1
p_0,2
p_1,2
p_2,2
As long as I understood, you want to convert matrix B to the matrix A in the following order:
v1= [p_0,0; p_0,1; ... p_0,N; p_1,0; ...; p_N,N]
v2=[p_0,0; p_1,0; ... p_N,0; p_0,1; ...; p_N,N]
where A*v1 equals to B*v2.
For this, you need swapping of each row of the matrix.
Try this code:
m=length(B); % m is equal to NxN
N=sqrt(m);
A=zeros(m,m);
for i=0:N-1
for j=0:N-1
A(j+i*N+1,:)=B(i+N*j+1,:);
end
end
I'm trying to solve a system of 10 linear equations out of which the middle 8 equations look alike. They look like this :
t_i-1 - 2.3086*(t_i) + t_i+1 == -7.7160
where i = 2:9
so I decided to construct the coefficient matrix and the constant matrix(array) for the system of equations through looping.This is what I did.
T = sym('t' , [1 10]); %% Creates a vector T = [ t1 t2 .... t10]
A_10 = zeros(10,10);
b_10 = zeros(10,1);
for i = 2:9 %% This loop generates the equations and arranges them in the matrices A_10 and B_10.
T(i-1) - 2.3086*T(i) + T(i+1) == -7.7160;
[A_10(i,i-1:i+1),b_10(i,1)] = equationsToMatrix(ans)
end
Everything except for the ninth row(last but one) is correct in the Matrix A_10. This is what A_10 looks like
A_10 =
Columns 1 through 9
0 0 0 0 0 0 0 0 0
1.0000 -2.3086 1.0000 0 0 0 0 0 0
0 1.0000 -2.3086 1.0000 0 0 0 0 0
0 0 1.0000 -2.3086 1.0000 0 0 0 0
0 0 0 1.0000 -2.3086 1.0000 0 0 0
0 0 0 0 1.0000 -2.3086 1.0000 0 0
0 0 0 0 0 1.0000 -2.3086 1.0000 0
0 0 0 0 0 0 1.0000 -2.3086 1.0000
0 0 0 0 0 0 0 1.0000 1.0000
0 0 0 0 0 0 0 0 0
Column 10
0
0
0
0
0
0
0
0
-2.3086
0
The last three elements of the row nine should be 1 , -2.3086 , 1 like the previous rows but it shows 1, 1, -2.3086. What am I doing wrong here?
This is what the iteration looks like in the loop
ans = t8 - (11543*t9)/5000 + t10 == -1929/250
The equation is correct too. I can't figure out what the problem is.
Without the second input vars, equationsToMatrix uses symvar to determine the variable list.
Using symvar directly with the last equation gives
>> i = 9;symvar(T(i-1) - 2.3086*T(i) + T(i+1) == -7.7160)
ans =
[ t10, t8, t9]
So for whatever reason, symvar produced the incorrect ordering for only the last equation (possibly because 1 < 9). To remedy the situation, pass your intended ordering using the second input
eqn = T(i-1) - 2.3086*T(i) + T(i+1) == -7.7160;
[A_10(i,i-1:i+1),b_10(i,1)] = equationsToMatrix(eqn,T(i-1:i+1));
You'll also noticed I assigned the equation to an explicit variable eqn. This is better practice than relying on ans.
Also, since you're producing a numeric array anyway, you can produce A without the Symbolic Toolbox in a number of ways. For example:
n = 10;
A = full(spdiags(ones(n,1)*[1,-2.3086,1],[-1,0,1],n,n));
A([1,end],:) = 0;
Good day,
In Matlab I have got a matrix which is very sparse. Now I would like to plot the 'density' of the matrix. Let's say I have a matrix A:
A = [3 0 0
0 2 0
0 0 1];
Now the plot should look something like:
x
x
x
So there should be a dot (or something else) at each location (row, column) in which matrix A has got a nonzero value.
Any ideas?
spy is what you need:
% taken from MatLab documentation
B = bucky;
spy(B)
Consider something like this:
subs = zeros(0,2);
for ind = [find(A)']
[r,c] = ind2sub(size(A), ind);
subs = [subs; [r,c]];
end
scatter(subs(:,2), subs(:,1));
set(gca,'YDir','Reverse')
xlim([1 size(A,2)])
ylim([1 size(A,1)])
Which, for the matrix A:
0 1 0 1 1
0 0 0 0 0
0 1 0 0 0
0 1 0 1 1
0 0 1 1 0
Gives you the following scatter plot:
What about this :
A=[3 0 0; 0 2 0; 0 0 1];
imagesc(A)