How can I write this line of code in MATLAB (currently R)? - r

How can I write this line of code in MATLAB (currently R)?
vcov_beta_hat <- c(sigma2_hat) * solve(t(X) %*% X)
My attempt is,
vcov_beta_hat = [sigma2_hat.*((X'*X))];
However I am struggling on what the 'c' is doing in the r code?

Whilst the above answer addresses that the solve is the something missing in your matlab code, solve can mean a number of different things in R,
If there is no comma in the equation its not solving anything and is actually taking the inverse,
Inverse of A, MATLAB: inv(A) R: solve(A)
Therefore, vcov_beta_hat = [sigma2_hat.*inv((X'*X))];

The c(a,b,c) denote a vector in R. In Matlab, you would write
vec = [a b c];
Also, you need to find the equivalent of the R-solve() function. So far, your matlab code just mutliplies X' with X and does not solve the system of equations.
linsolve should be a good starting point.

Related

Solving a linear system Ax=b with A being sparse matrix and b is a dense vector in Julia

I have a matrix A which is a sparse matrix stored in CSC format and a dense vector b. And I want to solve the system Ax = b for x. How can I do it?
Using \ operator or inv(A) both don't work.
I also tried factorize function as suggested in the link.
an anyone suggest an alternative method? Using Julia v1.7.1. Also, the matrix A is not tridiagonal. Please check the question at link for the error info.
Define "doesn't work". Perhaps with a minimum (non)working example.
For instance, I just tried this and it seemed to work beautifully:
julia> using LinearAlgebra, SparseArrays
julia> m = sparse(repeat(1:1000, inner=10), rand(1:1000, 10000), rand(10000));
julia> rhs = rand(1000);
julia> m \ rhs
1000-element Vector{Float64}:
-0.8976027048792632
1.451352807805229
3.6881731426288042
-0.8042571078676164
-2.7624771730498727
Without more information about your particular problem, there's nothing to be said.

Solving system of ODEs in vector/matrix form in R (with deSolve?)

So I want to ask whether there's any way to define and solve a system of differential equations in R using matrix notation.
I know usually you do something like
lotka-volterra <- function(t,a,b,c,d,x,y){
dx <- ax + bxy
dy <- dxy - cy
return(list(c(dx,dy)))
}
But I want to do
lotka-volterra <- function(t,M,v,x){
dx <- x * M%*% x + v * x
return(list(dx))
}
where x is a vector of length 2, M is a 2*2 matrix and v is a vector of length 2. I.e. I want to define the system of differential equations using matrix/vector notation.
This is important because my system is significantly more complex, and I don't want to define 11 different differential equations with 100+ parameters rather than 1 differential equation with 1 matrix of interaction parameters and 1 vector of growth parameters.
I can define the function as above, but when it comes to using ode function from deSolve, there is an expectation of parms which should be passed as a named vector of parameters, which of course does not accept non-scalar values.
Is this at all possible in R with deSolve, or another package? If not I'll look into perhaps using MATLAB or Python, though I don't know how it's done in either of those languages either at present.
Many thanks,
H
With my low reputation (points), I apologize for posting this as an answer which supposedly should be just a comment. Going back, have you tried this link? In addition, in an attempt to find an alternative solution to your problem, have you tried MANOPT, a toolbox of MATLAB? It's actually open source just like R. I encountered MANOPT on a paper whose problem boils down to solving a system of ODEs involving purely matrices.

qr function in R and matlab

I have a question about converting a matlab function into R, and I was hoping that someone could help.
The standard QR decomposition used in both matlab and R is referred to as qr(). To my understanding, the standard way of performing a qr decomposition in both languages is:
Matlab:
[Q,R] = qr(A) satisfying QR=A
R:
z <- qr(A)
Q <- qr.Q(z)
R <- qr.R(z)
Both of which provide me with the same results, unfortunately, this is not what I need. What I need is this:
Matlab:
[Q,R,e] = qr(A,0) which produces an economy-size decomposition in which e is a permutation vector so that A(:,e) = Q*R.
R:
No clue
I have tried comparing [Q,R,E] = qr(A) with
z <- qr(A);
Q <- qr.Q(z);
R <- qr.R(z);
E <- diag(ncol(A))[z$pivot]
and results seem identical for variables Q and E (but different for R). So depending on the defined inputs/outputs there will be different results (which makes sense).
So my question is:
Is there a way in R that can mimic this [Q,R,e]=qr(A,0) in Matlab?
I have tried digging into the matlab function but it leads to a long and torturous road of endless function definitions and I was hoping for a better solution.
Any help would be much appreciated, and if I've missed something obvious, I apologize.
I think the difference comes down to the numerical library underlying the calculations. By default, R's qr function uses the (very old) LINPACK routines, but if I do
z <- qr(X,LAPACK=T)
then R uses LAPACK and the results seem to match MATLAB's (which is probably also using LAPACK underneath). Either way we see the expected relationship with X:
z <- qr(X,LAPACK=F)
all.equal(X[,z$pivot], qr.Q(z)%*%qr.R(z), check.attributes=FALSE)
# [1] TRUE
z <- qr(X,LAPACK=T)
all.equal(X[,z$pivot], qr.Q(z)%*%qr.R(z), check.attributes=FALSE)
# [1] TRUE

troubles with integration on matlab

I'd like some help please I really need to solve this problem.
Well before anything thank you for your time...
My problem: I have a matrix (826x826 double) and I want to integrate this matrix with respect to a vector of (826x1 double) I don't have the functions of any of this. Is there a command or an algorithm to take the integral of a matrix with respect to a vector? Please I really need help, I'm such a newbie at matlab.
Sincerely.
George
If it's a constant matrix A integrated with respect to vector x, your answer in simply Ax + c where c is some constant vector. If A is a function of x, you will need to specify exactly what it is. Another case is when both A and x are functions of t. There is no one simple answer and no computer program would do it in most cases. There are books written in this stuff. It's not an easy task.
If I understand correctly, you have a matrix Y (size mxn) and a vector X (size mx1) where Y(i, j) = f_j(X(i)) for some unknown function f_j. To approximate the integral of each column over X you could use the trapz function of Matlab which uses the trapezoidal method.
A = trapz(X, Y);
This will integrate Y along its columns using the vector X. If you wanted to integrate along rows you can call the trapz function with an added argument of dim=2. Of course, the dimensions of X and Y must be compatible in either case.

Solving a matrix in MATLAB?

How does one solve the (non-trivial) solution Ax = 0 for x in MATLAB?
A = matrix
x = matrix trying to solve for
I've tried solve('A * x = 0', 'x') but I only get 0 for an answer.
Please note that null(A) does the same thing (for a rank-deficient matrix) as the following, but this is using the svd(A) function in MATLAB (which as I've mentioned in my comments is what null(A) does).
[U S V] = svd(A);
x = V(:,end)
For more about this, here's an link related to this (can't post it to here due to the formulae).
If you want a more intuitive feel of singular and eigenvalue decompositions check out eigshow in MATLAB.
You can use N = null(A) to get a matrix N. Any of the columns of N (or, indeed, any linear combination of columns of N) will satisfy Ax = 0. This describes all possible such x - you've just found an orthogonal basis for the nullspace of A.
Note: you can only find such an x if A has non-trivial nullspace. This will occur if rank(A) < #cols of A.
You can see if MATLAB has a singular value decomposition in its toolbox. That will give you the null space of the vector.
null(A) will give you the direct answer. If you need a nontrivial solution, try reduced row echelon form and refer the first page of the pdf.
R = rref(A)
http://www.math.colostate.edu/~gerhard/M345/CHP/ch7_4.pdf

Resources