matrix

Matrix of numbers javascript

倖福魔咒の 提交于 2020-01-02 07:32:47
问题 I need help with a function in JS that prints a matrix by a given integer N like this: N = 2; Matrix: 1 2 2 3 N = 3; Matrix: 1 2 3 2 3 4 3 4 5 I need to make it with 2 loops but I can't figure out how function solve(args) { var n = args[0]; } PS: Sorry for inserting the matrixes into JS code but that way I could visualise the result. 回答1: Here is the logic function paintMatrix(n) { for (var i = 1; i <= n; i++) { var result = ""; for (var j = 1; j <= n; j++) { result += (i + j - 1); } console

Change 1D vector (nx1) to 3D matrix (1x1xn)

落爺英雄遲暮 提交于 2020-01-02 07:18:07
问题 Given a= [1;2;3] I want to change this to b where b is b(1,1,1) = 1 b(1,1,2) = 2 b(1,1,3) = 3. How can I do this? Is there no built-in command for this? 回答1: Use permute to throw the first dimension back at the end as third dimension and bring the third and second dimensions to the front (their orders won't matter). Thus, we would have two such implementations, like so - permute(a,[3 2 1]) permute(a,[2 3 1]) You can also use reshape to push back the elements to the third dimension, like so -

Drawing two 3D strings on Canvas?

▼魔方 西西 提交于 2020-01-02 06:45:37
问题 I have to draw 2 Strings on canvas.Strings must be drawn with the same coordinates and second string must be result of rotating first string 45 degrees around axis Y.The result must looks like this: This is my code: Matrix matrix = new Matrix(); matrix = canvas.getMatrix(); mCamera = new Camera(); canvas.drawText("In the name of God", 30, 100, redPaint); mCamera.rotateY(45); mCamera.getMatrix(matrix); matrix.preTranslate(30, 100); // matrix.postTranslate(-30, -100); canvas.setMatrix(matrix);

How to efficiently generate lower triangle indices of a symmetric matrix

偶尔善良 提交于 2020-01-02 06:29:50
问题 I need to generate lower triangle matrix indices (row and columns pairs). The current implementation is inefficient (memory wise) specially when symmetric matrix gets big (more than 50K rows). Is there a better way? rows <- 2e+01 id <- which(lower.tri(matrix(, rows, rows)) == TRUE, arr.ind=T) head(id) # row col # [1,] 2 1 # [2,] 3 1 # [3,] 4 1 # [4,] 5 1 # [5,] 6 1 # [6,] 7 1 回答1: Here's another approach: z <- sequence(rows) cbind( row = unlist(lapply(2:rows, function(x) x:rows), use.names =

Apply lines() to columns of a data frame/matrix; each line with a different color

前提是你 提交于 2020-01-02 06:15:47
问题 I am trying to come up with a solution that doesn't involve using other packages such as ggplot. While plotting multiple lines is pretty straightforward, I haven't figured out a way to apply different values of an argument - e.g., different colors - to different lines. The code below (with the resulting plot) was my attempt, which obviously didn't do what I would like it to do. I also don't want to use a loop because I am trying to make my script as simple as possible. df = cbind(sort(rnorm

Mathematica dynamic to plot matrix data

青春壹個敷衍的年華 提交于 2020-01-02 04:44:47
问题 I am trying to dynamically plot data contained in a matrix with Mathematica 7. The data is contained in it like this, obtained via a chemical model. [year H He Li C ... C8H14+,Grain- ] [0 0 0.03 0.009 1E-3 ... 0 ] [100 .1 0.03 0.009 1E-3 ... 0 ] [200 .2 0.03 0.009 1E-3 ... 0 ] [300 .2 0.03 0.009 1E-3 ... 0 ] [... ... ... ... ... ... ... ] [1E6 .5 0.03 0.003 1E-8 ... 1E-25 ] The truth is, the matrix dimensions are 2001*1476 (2000 steps and first line for name, and 1475 compounds + 1 column for

For each element in a matrix, find the sum of all of its neighbors

心已入冬 提交于 2020-01-02 04:10:14
问题 Given a matrix, I want to find the sum of the neighbors for each element (so the result is a matrix). The neighbors are the values above, below and beside the given element ,if they exist (not considering the diagonal elements). Example: > z = matrix(1:9, 3, 3, byrow=T) > z [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 And the expected result is : > result [,1] [,2] [,3] [1,] 6 9 8 [2,] 13 20 17 [3,] 12 21 14 What is the simplest way I can do this in R without using loops? 回答1: One way

Use if clause in arrayfun in Octave/Matlab

China☆狼群 提交于 2020-01-02 04:05:15
问题 Is it possible to use "if" in arrayfun like the following in Octave? a = [ 1 2; 3 4]; arrayfun(@(x) if x>=2 1 else 0 end, a) And Octave complains: >>> arrayfun(@(x) if x>=2 1 else 0 end, a) ^ Is if clause allowed in arrayfun? 回答1: In Octave you can't use if/else statements in an inline or anonymous function in the normal way. You can define your function in it's own file or as a subfunction like this: function a = testIf(x) if x>=2 a = 1; else a = 0; end end and call arrayfun like this:

joining two numpy matrices

孤街醉人 提交于 2020-01-02 03:44:06
问题 If you have two numpy matrices, how can you join them together into one? They should be joined horizontally, so that [[0] [1] [[0][1] [1] + [0] = [1][0] [4] [1] [4][1] [0]] [1]] [0][1]] For example, with these matrices: >>type(X) >>type(Y) >>X.shape >>Y.shape <class 'numpy.matrixlib.defmatrix.matrix'> <class 'numpy.matrixlib.defmatrix.matrix'> (53, 1) (53, 1) I have tried hstack but get an error: >>Z = hstack([X,Y]) Traceback (most recent call last): File "labels.py", line 85, in <module> Z =

Correct use of pivot in Cholesky decomposition of positive semi-definite matrix

主宰稳场 提交于 2020-01-02 03:42:13
问题 I don't understand how to use the chol function in R to factor a positive semi-definite matrix. (Or I do, and there's a bug.) The documentation states: If pivot = TRUE, then the Choleski decomposition of a positive semi-definite x can be computed. The rank of x is returned as attr(Q, "rank"), subject to numerical errors. The pivot is returned as attr(Q, "pivot"). It is no longer the case that t(Q) %*% Q equals x. However, setting pivot <- attr(Q, "pivot") and oo <- order(pivot), it is true