Posts

Showing posts with the label matrix

How to create a matrix structure in C?

How to create a matrix structure in C? I'm a beginner in C, but I'm currently trying to create a matrix data structure that could be used in different functions without having to explicitly pass the number of columns and number of rows (example: matrixMult(matrix A, matrix B) instead of matrixMult(A, B, rowsA, columnsA, rowsB, columnsB) ). My approach so far has been to declare a struct such as matrixMult(matrix A, matrix B) matrixMult(A, B, rowsA, columnsA, rowsB, columnsB) typedef struct matrix{ int rows; int columns; int **data; }matrix; And then, to properly allocate the matrix, I'm trying to use the following function matrix startmatrix(matrix mat,int n_row, int n_col){ int i=0; mat.rows=n_row; mat.columns=n_col; mat.data=(int **) calloc(n_row,sizeof(int *)); for(i=0;i<n_row;i++){ mat.data[i]=(int *) calloc(n_col,sizeof(int)); } return mat; } Which (to my understanding) allocates the rows containing the columns, and ...

Three.js rotate vector and screen edge detection

Three.js rotate vector and screen edge detection I have a vector that I'm trying to keep length but rotate 90deg on colliding with the screen edge, but it gives me a weird out effect… don't know what I can be doing worn but it happens when I try to apply a Matrix and a Euler transform… Also for the screen detection, what I have is okj for detecting on and off screen, but it would be handy to know if it's the bottom, top, righ or left edge… Any clues? Thanks! var direction = new THREE.Vector3(-0.2, -0.2, 0); var a = new THREE.Euler( (Math.PI/2), 0, 0, 'XYZ' ); direction.applyEuler(a); For the collision; I'm using the following: camera.updateMatrixWorld(); // make sure the camera matrix is updated camera.matrixWorldInverse.getInverse( camera.matrixWorld ); cameraViewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); frustum.setFromMatrix( cameraViewProjectionMatrix ); console.log(frustum.intersectsObject(textMesh)); ...

How to impliment logical indexing faster in matlab? [closed]

How to impliment logical indexing faster in matlab? [closed] I have several matrices that I need to add to one large matrix. The large matrix (300002x50) is split up by .001 seconds and the timing for the other 49 matricies (14250x2) are roughly .02 apart, but not uniformly distributed. I have tried find to index the entries from the smaller matrices into the larger matrix, but it was too slow. I have since tried: find for a = 1:length(test) aaa = abs(AF1(:,1)-test(a,1))<10^-6; AF1(aaa,index)=test(a,2); end Where test is a 14250x2 double (time,data) , AF1 is a 300002x50 double matrix and index is which column in AF1 the data will be added to. It was a bit faster, but it still takes up 99.3% (29 minutes) of the time. It works how I want it, but is there any way to implement this in a faster manner? test (time,data) AF1 index AF1 Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. A...

Creating an array with data conditional on another matrix

Creating an array with data conditional on another matrix I know there must be an apply function or ave for this, but I am not quite sure how to do it: I have data: date player market 1: 1-1 1 1 2: 1-1 2 1 3: 1-1 1 2 4: 1-2 2 1 5: 1-2 3 2 6: 1-3 21 1 7: 1-4 1 1 8: 1-4 51 1 9: 1-4 1 1 10: 1-5 1 2 I also have a blank array, which has unique dates on the rows, unique markets on the columns, and unique players for the third dimension. 1 [,,1] 1 2 1-1 1-2 1-3 1-4 1-5 2 [,,2] 1 2 1-1 1-2 1-3 1-4 1-5 etc I want to fill out the array with from the data. I want each point to = 1 if the guy has an entry in the data where he is present for a date and market combination, and 0 if not. So for example, for 1 and 2, they would be filled out as: 1 [,,1] 1 2 1-1 1 1 1-2 0 0 1-3 0 0 1-4 0 1 1-5 0 1 2 [,,2] 1 2 1-1 1 0 1-2 1 ...

Logical arrays as indices to arrays to convert elements to zero in MATLAB

Logical arrays as indices to arrays to convert elements to zero in MATLAB I am trying to convert some elements of a matrix to zeros depending on a logical array. Suppose we have a random 5 x 5 matrix: 5 x 5 b = 0.0344 0.1869 0.7547 0.1190 0.2238 0.4387 0.4898 0.2760 0.4984 0.7513 0.3816 0.4456 0.6797 0.9597 0.2551 0.7655 0.6463 0.6551 0.3404 0.5060 0.7952 0.7094 0.1626 0.5853 0.6991 And I have an array of zeros and ones with the same dimension: a = 0 1 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 1 1 0 Doing a(logical(b)) gives me the elements I am looking for, but in a vector form: a(logical(b)) ans = 0.4387 0.3816 0.1869 0.4456 0.6463 0.7547 0.2760 0.6551 0.1626 0.4984 0.9597 0.5853 0.2551 0.5060 How can I get the following matrix instead? 0 0.1869 0.7547 0 0 0.4387 0 0.2760 0.4984 0.7513 0.3816 0.4456 ...

Transforming Coordinates from EASE-Grid to Lat/Lon in R

Transforming Coordinates from EASE-Grid to Lat/Lon in R I have 3 matrices, all 721x721. The first contains global snow cover data in the NSIDC's EASE-Grid, the second is the latitude corresponding to each cell of the EASE-Grid and the third is the longitude corresponding to each cell of the EASE-Grid. For example, the latitude of the snow data in cell [x,y] is given in cell [x,y] of the latitude matrix. I want to transform the coordinate system from the EASE-Grid into the regular Lat/Lon grid given in the latter two matrices, essentially reordering all the cells in the snow data matrix based on the latitude and longitude matrices. Any ideas how to do this? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.