How to multiply two vector and get a matrix?

Normal matrix multiplication works as long as the vectors have the right shape. Remember that * in Numpy is elementwise multiplication, and matrix multiplication is available with numpy.dot() (or with the @ operator, in Python 3.5) >>> numpy.dot(numpy.array([[1], [2]]), numpy.array([[3, 4]])) array([[3, 4], [6, 8]]) This is called an “outer product.” You can get it … Read more

What does a circled plus mean?

People are saying that the symbol doesn’t mean addition. This is true, but doesn’t explain why a plus-like symbol is used for something that isn’t addition. The answer is that for modulo addition of 1-bit values, 0+0 == 1+1 == 0, and 0+1 == 1+0 == 1. Those are the same values as XOR. So, … Read more

Matrix from Python to MATLAB

If you use numpy/scipy, you can use the scipy.io.savemat function: import numpy, scipy.io arr = numpy.arange(9) # 1d array of 9 numbers arr = arr.reshape((3, 3)) # 2d array of 3×3 scipy.io.savemat(‘c:/tmp/arrdata.mat’, mdict={‘arr’: arr}) Now, you can load this data into MATLAB using File -> Load Data. Select the file and the arr variable (a … Read more

Scipy sparse… arrays?

Use a scipy.sparse format that is row or column based: csc_matrix and csr_matrix. These use efficient, C implementations under the hood (including multiplication), and transposition is a no-op (esp. if you call transpose(copy=False)), just like with numpy arrays. EDIT: some timings via ipython: import numpy, scipy.sparse n = 100000 x = (numpy.random.rand(n) * 2).astype(int).astype(float) # … Read more

Converting two lists into a matrix

The standard numpy function for what you want is np.column_stack: >>> np.column_stack(([1, 2, 3], [4, 5, 6])) array([[1, 4], [2, 5], [3, 6]]) So with your portfolio and index arrays, doing np.column_stack((portfolio, index)) would yield something like: [[portfolio_value1, index_value1], [portfolio_value2, index_value2], [portfolio_value3, index_value3], …]

Remove a column from a matrix in GNU Octave

In case you don’t know the exact number of columns or rows you can use the magic “end” index, e.g.: mymatrix(:,2:end) % all but first column mymatrix(2:end,:) % all but first row This also allows you to slice rows or columns out of a matrix without having to reassign it to a new variable.