Adding Matrices in Matlab with out looping [closed] - matlab

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 7 years ago.
Improve this question
how to sum m-by-n matrix A, to m by n by p matrix B, using Matlab without a For loop. Where the result C should be m by n by p matrix, the direct addition results in
Error using +
Matrix dimensions must agree.
?

If you are looking to add A to each of the p slices of B then you should use bsxfun:
bsxfun(#plus,A,B)

First modify A to have the same size of B by replicating A, p times:
A = repmat(A ,[1 1 p]);
Now A is m by n by p the summation then can be done as
C = A + B
Where C is a m by n by p matrix

Related

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.

MATLAB create a matrix M = F(i,j) [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 7 years ago.
Improve this question
Suppose I have a function of 2 variables F(i,j) which depends on the row index and column index of a matrix, and I want to fill the matrix with the values M_ij = F(i,j)
Of course it's possible to do a loop through i and j, or even only i or j if the function F can be vectorized, but I'd like to know the neat way to do that.
It's impossible to answer without seeing your F but let's assume that F is vectorized such as
F = #(x,y)x+y;
then you could use ndgrid:
[I,J] = ndgrid(1:m,1:n);
M = F(I,J)
In the above case, and this may well apply in your case as well, you might be able to vectorize the function directly using something like bsxfun:
M = bsxfun(#plus, 0:m-1, 1:n);
Regardless of whether your function F is vectorized or not, you have to evaluate it for every value of i and j. If F is not vectorized, you will have to do a loop over the indices manually. If F is based on MATLAB builtins like sin, log, etc., it is most likely vectorized. In this case, you can pass in i and j that are the same size as M and get the result in one step:
[j, i] = meshgrid(1:size(M, 2), 1:size(M, 3))
M = F(i, j)
Note that meshgrid takes and returns the parameters as X, Y, which is the opposite of matrix indexing order row, col.

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

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

AX=0 and introducing error matrix [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 9 years ago.
Improve this question
I'm having a matrix of 10 rows and 5 columns, thatr I have called A
I would like to make AX=0 and determine X which contains 5 unknown parameters.
What I did is
null(A)
But it seems that it's considering what I did as a linear algebra.
I would like to introduce another matrix which contains a matrix of errors . which would give me a priori:
AX + E = 0
because the results are not accurate, indeed, but still, I would like to find the closest parameters of the unknown vector (X) and get an error matrix.
Can you help me with that?
You can do eigen value decomposition of A^T.
[Q, D] = eigen(A^T)
And take the eigen vectors that corresponds to the smallest eigen value.
That is take y vectors from the right hand side of the Q matrix.
y is the column number of X.
And then E = -AX
====edit=====
OK. actually you want to minimize E. But since E is a matrix I assume you want to minimize the sum of squares of all the elements in E.
That is:
||E||_F^2 = ||-AX||_F^2 = trace(X^T(A^TA)X)
You can do eigen decomposition of A^TA
[Q, D] = eigen(A^TA)
QA^TA = DQ => D = QA^TAQ^T
To make it smaller, X should be the columns of Qs that corresponds to the smallest eigen value in the diagonal of D.
If there is a eigen value equals to 0, then AX can be 0 and E=0