I have an iterator that iterates through a vector and I need to append values into an armadillo matrix. How do i go about doing that?
you can use
insert_rows(row_number, X)
insert_cols(col_number, X)
depending on how you are adding elements. Ideally, you should define the size of your matrix at declaration, and iterate through your matrix using element access, i.e.:
[],
(), or
.at()
Related
How do you create an array of (variable)matrices A1,..,AN of semi definite matrices in Julia Jump where N is a parameter? #variable(model,x[1:N]) won't work because this is an array of variables, not of matrix variables.
Thanks in advance.
It's best to use the anonymous variable syntax here.
#variable(m, [1:N,1:N], SDP)
returns one matrix N x N of variables which is symmetric and PSD constrained. If you want a collection of K of them, just make one:
A = [#variable(m, [1:N, 1:N], SDP) for k in 1:K]
I have a rank one array given by
COMPLEX(KIND = DBL),DIMENSION(DIMJ)::INITIALSTATE
How can I covert its rank to
DIMENSION(DIMJ,1)
so I can perform matrix operations on it -- transpose etc.
Note that the change is trivial. In both cases, we have a column vector. But Fortran won't trasnpose the array in the first form. Assume that DIMJ is an initialized integer.
Also, as obvious, I'd like the complex numbers to remain intact in the right positions after the manipulation.
Is it possible to perform such an operation in fortran?
If you just want a temporary read-only version of the array, you can always use RESHAPE:
TRANSPOSEDSTATE = transpose(reshape(INITIALSTATE, (/ DIMJ, 1 /)))
But this doesn't change the shape of the INITIALSTATE variable. Also, that is the same as
TRANSPOSEDSTATE = reshape(INITIALSTATE, (/ 1, DIMJ /))
no transpose needed.
I'm looking for a possibility to specify matrix quantities that depend on a variables. For scalars that works as follows, using undefined functions:
from sympy import *
x = Function('f')(t)
diff(x,t)
For Matrix Symbols like
x = MatrixSymbol('x',3,3)
i cannot find an equivalent. There is
i,j = Symbols('i j')
x = FunctionMatrix(6,1,Lambda((i,j),f))
but this is not what i need as you need to specify the contents of the matrix. The context is that i have equations
which should be derived in time and contain matrix valued elements.
I cannot deal with the elements of the matrices one by one.
Thanks!
I'm not sure about what you want, but I think you want to make a Matrix with differentiable elements. In that case, see if this works for you.
Create a matrix with function elements:
X = sym.FunctionMatrix(6,1,lambda i,j:sym.Function("x_%d%d" % (i,j))(t))
M = sym.Matrix(X)
M.diff(t)
This results in
Matrix([
[Derivative(x_00(t), t)],
[Derivative(x_10(t), t)],
[Derivative(x_20(t), t)],
[Derivative(x_30(t), t)],
[Derivative(x_40(t), t)],
[Derivative(x_50(t), t)]])
You may then replace stuff as you need.
Also, it may be preferrable if you populate the matrix with the expressions you need before differentiating. Leaving them as undefined functions may make it harder for you to simplify after substitution.
I have a struct like this
temp_struct(1).budget=8
temp_struct(2).budget=8
and I want to subtract a constant value from both of them (replacing 8 with the new value). How can i do it more efficiently without using a loop in matlab?
%extract a cs list and convert it to a vector, then apply the operation you want in a vectorized manner
a=[temp_struct(:).budget]-42
%convert to cell because there is no direct way from vector to cs list
a=num2cell(a)
%use a cs list to assign the values.
[temp_struct(:).budget]=a{:}
What is a cs list?
How do I sort one vector based on the values of another?
Say I have predefined order:
(def order ["0M","6M","1Y","2Y","3Y"])
I have another vector ["0M","1Y","6M"] (may or may not contain all elements of vector "order")
Output should be ["0M","6M","1Y"]
(def order ["0M","6M","1Y","2Y","3Y"])
(sort-by #(.indexOf order %) ["0M", "1Y", "6M"]) ; ("0M" "6M" "1Y")
Notice that sort-by returns a sequence. If you absolutely need a vector result, you can feed the output to vec.