matrix

Insert vector into matrix at specific columns

こ雲淡風輕ζ 提交于 2021-02-05 08:31:11
问题 How would I insert a vector b into a matrix at column col? I cannot find any syntax for and insert or append function in Fortran. So far all I have done is reassigned the values in the column, but I only want to insert the vector. real :: M(n,n) integer :: n, col real :: b(n) M(n:col) = b(:) 回答1: If I understood of your problem, you want to: increase the number n of columns of the matrix m by 1; insert the content of a vector b in m as a new column, at index col ; right-shift the remaining

Optimal path through a matrix with multiple costs to consider

蓝咒 提交于 2021-02-05 07:55:09
问题 For example given the below matrix: [[[0, 8], [0, 3], [0, 8]], [[8, 0], [3, 0], [0, 5]], [[0, 1], [0, 6], [0, 0]]] where for each tuple the first number is food and the second number is water. I need to get from the bottom right to the top left and I can only move up or left. I need to gather as much food and water as possible so I can survive as long as possible. For each day I want to survive I need 1 food and 1 water so if I could choose between a path that would result in (7,4) and one

Create a frequency matrix for bigrams from a list of tuples, using numpy or pandas

穿精又带淫゛_ 提交于 2021-02-05 07:47:27
问题 I am very new to Python. I have a list of tuples, where I created bigrams. This question is pretty close to my needs my_list = [('we', 'consider'), ('what', 'to'), ('use', 'the'), ('words', 'of')] Now I am trying to convert this into a frequency matrix The desired output is consider of the to use we what words consider 0 0 0 0 0 0 0 0 of 0 0 0 0 0 0 0 0 the 0 0 0 0 0 0 0 0 to 0 0 0 0 0 0 0 0 use 0 0 1 0 0 0 0 0 we 1 0 0 0 0 0 0 0 what 0 0 0 1 0 0 0 0 words 0 1 0 0 0 0 0 0 How to do this,

Matrix of averages from array

主宰稳场 提交于 2021-02-05 06:43:26
问题 Right now i have an array that is three dimensions. I have 200 rows, 200 columns and 24 "slices" in the third dimension dim=c(200,200,24) What I need is an average of the slices resulting in a new matrix. I need a 200 by 200 matrix and the values are the result of averaging up the appropriate slices. So in the location that would be row 1, col 1, I need the average of all the row 1's and col 1's from my array. Is there a way to do this? 回答1: Here's one attempt using a simple example: test <-

sum of multiplication of two columns of a matrix in R

China☆狼群 提交于 2021-02-05 05:53:23
问题 I am generating a matrix in R using following, ncolumns = 3 nrows = 10 my.mat <- matrix(runif(ncolumns*nrows), ncol=ncolumns) This matrix indicates the co-ordinates of a point in 3D. How to calculate following in R? sum of x(i)*y(i) e.g. if the matrix is, x y z 1 2 3 4 5 6 then output = 1*2 + 4*5 I'm trying to learn R. So any help will be really appreciated. Thanks 回答1: You're looking for the %*% function. ncolumns = 3 nrows = 10 my.mat <- matrix(runif(ncolumns*nrows), ncol=ncolumns) (my

converting the index of 1D array into 2D array

妖精的绣舞 提交于 2021-02-04 21:37:18
问题 How can I convert the index of 1D array into 2D array? I know how to convert a 2D array into 1D (i*the size of row+j) . I want the opposite of that. 回答1: What you need to know is: How many columns should the 2D array have: Lets say you have an array of 20 columns and 10 rows (array[20,10]): int index = 47; int numberOfColumns = 20; int column = index % numberOfColumns; int row = index / numberOfColumns; // column == 7 // row == 2 回答2: You can just do the opposite. if n is the length of the

How to convert a monodimensional index to corresponding indices in a multidimensional array?

浪子不回头ぞ 提交于 2021-02-04 21:08:22
问题 Say I have a 3x4x5x6 java double array a that I unroll into an ArrayList b of length 360 in the following way: for (int i = 0; i<a.length; i++){ for (int j = 0; j<a[0].length; j++){ for (int k = 0; k<a[0][0].length; k++){ for (int m = 0; m<a[0][0][0].length; m++){ b.add(a[i][j][k][m]); } } } } Given the index of b , is there an easy way to find the corresponding 4-tuple of indices in a ? 回答1: Assuming that b is the index on the monodimensional array i,j,k,m are the four resulting indices on

How to convert a monodimensional index to corresponding indices in a multidimensional array?

房东的猫 提交于 2021-02-04 21:07:51
问题 Say I have a 3x4x5x6 java double array a that I unroll into an ArrayList b of length 360 in the following way: for (int i = 0; i<a.length; i++){ for (int j = 0; j<a[0].length; j++){ for (int k = 0; k<a[0][0].length; k++){ for (int m = 0; m<a[0][0][0].length; m++){ b.add(a[i][j][k][m]); } } } } Given the index of b , is there an easy way to find the corresponding 4-tuple of indices in a ? 回答1: Assuming that b is the index on the monodimensional array i,j,k,m are the four resulting indices on

Matlab - matrix to the power of 2

可紊 提交于 2021-02-04 19:55:08
问题 In Matlab , I have typed the following commands: >> a = [1 2; 3 4] a = 1 2 3 4 When I tried the command a^2 , I got the following: >> a^2 ans = 7 10 15 22 I was actually expecting to get: ans = 1 4 9 16 In other words, I was expecting to get each element of the matrix to be raised to 2 . Why was the result as shown above? Thanks. 回答1: In MATLAB, all single-character operators are matrix operators. So, you are using the matrix power, e.g., a^2 == a*a if you want to square each element , you'll

Java 8 matrix * vector multiplication

最后都变了- 提交于 2021-02-04 17:38:29
问题 I'm wondering if there is a more condensed way of doing the following in Java 8 with streams: public static double[] multiply(double[][] matrix, double[] vector) { int rows = matrix.length; int columns = matrix[0].length; double[] result = new double[rows]; for (int row = 0; row < rows; row++) { double sum = 0; for (int column = 0; column < columns; column++) { sum += matrix[row][column] * vector[column]; } result[row] = sum; } return result; } Making an Edit. I received a very good answer,