matrix

MatLab - Applying a function to each row in a matrix

北慕城南 提交于 2020-01-04 04:46:27
问题 I have a matrix with rows of 4 integers, with an unspecified number of columns (depends on the text file). I'm wanting to apply a function to each row of the matrix, independently. The function has 4 inputs, and 2 outputs. I'm trying to use the arrayfun function to do this, but whenever I call the function, I get an error saying: "Not enough input arguments." Here is the function call: [gain,phase]=arrayfun(@(x) GainPhaseComp(B(x,1:4)), 1:size(B)); where b is an n by 4 matrix. Here is the

Issue with GLM Camera X,Y Rotation introducing Z Rotation

雨燕双飞 提交于 2020-01-04 04:41:06
问题 So I've been having trouble with a camera I've implemented in OpenGL and C++ using the GLM library. The type of camera I'm aiming for is a fly around camera which will allow easy exploration of a 3D world. I have managed to get the camera pretty much working, it's nice and smooth, looks around and the movement seems to be nice and correct. The only problem I seem to have is that the rotation along the camera's X and Y axis (looking up and down) introduces some rotation about it's Z axis. This

Efficient element-wise matrix division when elements in denominator may be zero

狂风中的少年 提交于 2020-01-04 04:40:15
问题 I'm programming with Python 2.7.6 using numpy. I have this division between two numpy matrixes V/np.dot(W,H) . Sometimes happens that the denominator has some cell values equal to 0, so i get a Runtime error. I would like to implement a safe division in a efficient way. How can i write a code that performs the Matrix division and for the elements where the denominator is equal to 0 puts 0 in the output Matrix? 回答1: Numpy actually allows you to set what you'd like to do in the case of a divide

Put pairwise differences of matrix rows in 3-d array

▼魔方 西西 提交于 2020-01-04 04:14:08
问题 I have a matrix Y of shape (n, d). I already calculated the pairwise row-differences in the following way: I, J = np.triu_indices(Y.shape[0], 0) rowDiffs = (Y[I, :] - Y[J, :]) No i want to create a 3d-array, containing the differences of the rows i and j of Y at position (i,j, :). How would you do it? The aim of it is to replace this inefficient loop: for i in range(Y.shape[0]): for j in range(Y.shape[0]): C[i,:] = C[i,:] + W[i, j] * (Y[i, :]-Y[j, :]) 回答1: I have found some success with this:

How to dynamically reshape matrix block-wise? [duplicate]

China☆狼群 提交于 2020-01-04 02:33:07
问题 This question already has answers here : Collapsing matrix into columns (8 answers) Closed 3 years ago . Let's say I have A = [1:8; 11:18; 21:28; 31:38; 41:48] Now I would like to move everything from column 4 onward to the row position. How do I achieve this? A = 1 2 3 4 5 6 7 8 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27 28 31 32 33 34 35 36 37 38 41 42 43 44 45 46 47 48 to A2 = 1 2 3 4 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 5 6 7 8 15 16 17 18 35 36 37 38 45 46 47 48 reshape

Extract series of successive vectors from matrix

◇◆丶佛笑我妖孽 提交于 2020-01-04 02:19:05
问题 I have a large matrix, say for simplification: > mat = matrix(c(1:50), ncol = 5) > mat [,1] [,2] [,3] [,4] [,5] [1,] 1 11 21 31 41 [2,] 2 12 22 32 42 [3,] 3 13 23 33 43 [4,] 4 14 24 34 44 [5,] 5 15 25 35 45 [6,] 6 16 26 36 46 [7,] 7 17 27 37 47 [8,] 8 18 28 38 48 [9,] 9 19 29 39 49 [10,] 10 20 30 40 50 I want to extract successively all possible vertical vectors composed of 3 elements from each column, e.g. (1,2,3), (2,3,4),...,(11,12,13), (12,13,14),... and generate a 3-by-X matrix composed

Can characters be used as indices?

此生再无相见时 提交于 2020-01-04 01:01:10
问题 Let's define, for example, x = 10:10:2000; As is well known, integer values can be used as indices: >> x(9) ans = 90 In Matlab, characters can often be used where a number would be expected, with Matlab doing the conversion automatically. For example, since the ASCII code of 'a' is 97 , >> 'a'+1 ans = 98 Can characters be also used as indices? Does Matlab convert them into integers? 回答1: They can be used... but careful if the index is a single colon! Let's define >> x = 10:10:2000; Indexing

Reshape matrix by rows

我们两清 提交于 2020-01-03 21:02:24
问题 I have a matrix with size 18000 x 54. I would like to reshape it as a matrix with size 54000 x 18, in which each row of my initial matrix becomes a matrix which has 3 rows. Let's take an example. I have a matrix as follow: a = matrix(1:18, nrow = 2, ncol = 9, byrow = T) a [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 I would like to reshape this matrix so that it becomes: [,1] [,2] [,3] 1 4 7 2 5 8 3 6 9 10 13 16 11 14 17 12 15 18 I tried two

What happened to Microsoft.FSharp.Math.Matrix?

房东的猫 提交于 2020-01-03 19:15:14
问题 Kind of strange how this class type has dropped of the F# released with VS2010 Does anyone know where it has gone? or where it is now? 回答1: It is in the F# powerpack, which is used by the F# team for "unstable" code that will get released and updated more often than the main release of F# and libraries. (Note that the code is still high-quality code, you should ignore most of the connotations that "unstable" has in this case). It's just a way for the F# team to get more experimental code out

Why doesn't numpy determinant return a Fraction when given a Fraction matrix?

▼魔方 西西 提交于 2020-01-03 18:52:31
问题 I want to perform operations on rational matrices. I use the modules numpy and fractions . Here is my code: import numpy as np from fractions import Fraction m=np.matrix([[Fraction(1, 6), Fraction(8, 7)], [Fraction(1, 2), Fraction(3, 2)]]) print(np.linalg.det(m)) # Gives -0.321428571429 print(m[0,0]*m[1,1] - m[0,1]*m[1,0]) # Gives -9/28 Since computing the determinant only require rational operations with the Gauss' method, the determinant of a rational matrix is rational. So my questions are