How do i obtain only the first principal component in MATLAB? [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
For certain measurements i need to obtain only the numeric value of the first principal component from the matrix. Can someone please tell me how do i go about it?

the most straight forward way is just to get the top eigenvector/value of your data's covariance matrix using eigs
say the data matrix x is N by D, or # of data by dimension of data
you can simply do
C = cov(X);
[V, D] = eigs(C, 1);
in fact, you can get the top k principal components by running
[V, D] = eigs(C, k);

Related

Generation of coordinates from a range of values in matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
distance = [10:.1:30];
norm_dist = normpdf(distance,20,2);
I am trying to generate x,y,z coordinates from the above range of normally distributed values but don't know how to do. Please help.
The normal_dist variable generates values in a normally distributed fashion. I want to use these values to randomly generate the x,y,z, coordinates values. The separate x,y,z values need to be generated not a 3-D array
If I understand your question correctly, you need to use meshgrid function as well
distance = [10:.1:30];
[X,Y,Z] = meshgrid(distance);
F = normpdf(X,20,2);
As a result you'll get 3d grid with those numbers
meshgrid() creates a matrix you can use for further calculations. Probably you will need to make normpdf dependent on X, Y, Z at the same time, e.g.
F = normpdf(X.^2+Y.^2+Z.^2,20,2)

MATLAB plot of 2 variables defined by an equation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have the following query. I want to plot in MATLAB the following equation :
i + s - ln(s)/sigma = constant (i and s are variables)
for a given value of constant and sigma. The equation is between s and i. the value of sigma is 0.5 and value of constant can be assumed to be 1.
I want to plot the above equation. i and s both are function of time but in graph we need graph of s & i only. i on y axis and s on x axis.
try fimplicit
https://www.mathworks.com/help/matlab/ref/fimplicit.html
f=#(s,i) i + s - log(s)/2 - 5;
fimplicit(f,[1 5 2 10])

Matlab: equivalent of R's matrix multiplication (A %*% B)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Working in Matlab with time-homogenous Markov Chains and looking to figure out how I can perform matrix multiplication in Matlab for matrix A, similar to R's matrix multiplication, i.e., A %*% A. It would be even better if I could perform A^n instead for a given n instead of having to use A %*% A %*% A, when n = 3, for example.
Any help is greatly appreciated!
First of all you can raise a Matrix to a power in MATLAB:
A ^ n = A * A * A * ... * A
Actually MATLAB uses pretty sophisticated algorithms behind the scene to accelerate this.
For example, if the Matrix is diagonalizable, MATLAB will use that to accelerate the calumniation.

Multiple linear system, same solution [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I know how to solve the problem
Ax=B
with matlab, I just use mldivide to obtain x: x=A\B
But what if I have multiple basis A_i and multiple data B_i but the nature of the problem suggests me that the solution x must be the same for every i?
You could try stacking the A matrices and B vectors to obtain a larger least squares system. That is, form
A = (A_1)
...
(A_n)
and
B = (B_1)
...
(B_n)
and then solve
A*x = B
in the least squares sense
The solution x to such a system will be the value that minimises
Sum{ || A_i*x - B_i ||^2 }
If I understand correctly, this is an image "unmixing" problem, which requests the solution of a (highly) overdetermined system of W x H equations (image area) in K unknowns (number of end members).
One wants to solve
X1.U1ij + X2.U2ij + X3.U3ij = Vij
(assuming K=3) where i, j cover the whole image.
The standard solutions will be least-squares minimization, with two caveats:
if there are outliers the results canbe badly biased and robust methods should be preferred;
if this is really a mixture problem, then the coefficients are constrained to be positive and the question should be recast as a linear programming problem.

Random matlab matrix with at least .4 and at most .6 ones per column [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm creating a matrix in matlab of size n that contains only ones and zeros. The easiest way to do that is round(rand(m,n)) for a matrix of size mxn, but it creates in some cases rows that have all zeros or all ones. I want to put a lower and upper bound in the number of ones that each row has. Is there an easy way to do that?
Thanks
This is just for one column, but can easily be extended to a matrix:
v = zeros(m,1); % column
Fill the beginning of the column with at least 40% and at most 60% ones:
v(1: floor((0.4+(0.6-0.4)*rand())*(m+1))) = 1;
Shuffle the column:
v = v(randperm(numel(v)));