matrix

Changing values in a matrix depending on whether they are above of below a certain threshold value

放肆的年华 提交于 2021-01-29 09:32:14
问题 I have a matrix. If an element of the matrix is greater than 400, then I would like the element to become zero. If an element of the matrix is less than 400, then I would like to multiply the element by three. Here is how to reproduce my matrix: structure(c(122, 948, 952, 100, 942, 150, 150, 149, 244, 220, 437, 395, 356, 473, 434, 335, 357, 371, 590, 553, 520, 491, 426, 426, 427, 177, 284, 338, 391, 290, 345, 399, 143, 193, 136, 121, 122, 187, 177, 544), .Dim = c(10L, 4L), units = structure

R distance matrix build

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-29 08:22:55
问题 New to R. I have a matrix of coordinates of several components in R, looks like: x y z C1 0.3 0.2 -1.2 C2 -1.5 0.7 0 C3 0.2 -0.75 0.22 ... My question is how to build a distance matrix of pairs of each components in R like: C1 C2 C3 ... C1 0 0.2 0.7 ... C2 0.2 0 1.2 ... C3 0.7 1.2 0 ... ... 回答1: You would do as.matrix(dist(Matrix)) Then: rownames(DistMatrix) <- colnames(DistMatrix) <- rownames(Matrix) 来源: https://stackoverflow.com/questions/13843048/r-distance-matrix-build

Check value of neighbouring squares in 2D array

你离开我真会死。 提交于 2021-01-29 07:27:32
问题 I am creating a simple mine-sweeper board with 2D array. I wants to populate the squares surrounding the "Bomb(s)" with respective numbers. I don't need to care whether the board I created can be solved. (Not important here) My question is: How can we populate the numbers elegantly without listing out all the possibilities as if we were hard-coding? What I came out with for now is a massive nested if-statements to check whether the neighbour is -Inside bounds of array -Is it a bomb -Is it

Implementation of methods of interface

空扰寡人 提交于 2021-01-29 07:17:31
问题 I want to implement a representation of matrices. for that I have two types of matrices - regular and sparse, which differ in their implementation - one holds a vector, and the second a map of indices and value, both inherit from Matrix class. For that, I'm using the strategy pattern, where I create the base abstract class Matrix , two classes that inherit from Matrix - RegMatrix and SparseMatrix , and MyMatrix that holds a pointer to a Matrix . I want to implement the + operator, which

Stimulating Liquid Flow on Matrix

放肆的年华 提交于 2021-01-29 06:50:31
问题 I have to simulate a liquid flow through a square matrix that contains a set of integers. The liquid should start from the top left corner of the matrix. It could only move towards right or below adjacent matrix. The lower value of the adjacent matrix, the higher potential for it to flow. The movement of the liquid is considered stop at the right or below edge of the matrix. The program should be able to show the sum of all numbers that the liquid has passed through. import numpy as np mtr=

Read 2D matrix from file to 2D int array in C#

末鹿安然 提交于 2021-01-29 04:40:42
问题 I have problem in reading 2D text from file and import it to an int array. Specifically, my text file looks like below: 2,3,4,5,6 5,2,3,4,5 2,4,6,7,4 2,7,8,5,6 So each cell in matrix is separated by comma and each new row starts with new line. I tried many ways to make it works but I can't! Simply, I want an int[][] or int[,] array at the end. P.S: I can read 1-D matrix simply to int[] as below: int[] array= File.ReadAllText(fileppath).Split(',').Select(t => int.Parse(t)).ToArray(); 回答1: //

How to add 1 to every element of a matrix / nested list in Python?

僤鯓⒐⒋嵵緔 提交于 2021-01-29 04:32:05
问题 I am currently working on a problem that requires me to take a matrix / nested list, and add 1 to every element of this list, and then return the new modified list. I also have to make it so that a user can input the matrix/nested list of their choice. For example, the person would enter: [[1,2,3],[4,5,6]] and the program would return [[2,3,4],[5,6,7]] So far I have this code: m = int(input("Enter the number of rows: ")) matrice = [] i=0 while (i<m): print("Enter the row", i,"(separate each

How to add 1 to every element of a matrix / nested list in Python?

拜拜、爱过 提交于 2021-01-29 04:24:40
问题 I am currently working on a problem that requires me to take a matrix / nested list, and add 1 to every element of this list, and then return the new modified list. I also have to make it so that a user can input the matrix/nested list of their choice. For example, the person would enter: [[1,2,3],[4,5,6]] and the program would return [[2,3,4],[5,6,7]] So far I have this code: m = int(input("Enter the number of rows: ")) matrice = [] i=0 while (i<m): print("Enter the row", i,"(separate each

Led Matrix Arduino

青春壹個敷衍的年華 提交于 2021-01-29 03:10:01
问题 I am not an electrical engineer, however I am putting together an Arduino Nano Led Matrix 40x20 Led grid, I was wondering what power supply I should use so everything works properly without destroying anything. This is what was recommended for a 10x10 grid. 5V 4A Power Supply: http://amzn.to/1UhdJfB I will also be using WS2812B LEDs if that makes any difference. Also if you know a good software to make the led animations that would be great too, the software I have is limiting. 回答1: According

Efficiently finding non zero numbers from a large matrix

半世苍凉 提交于 2021-01-29 02:19:11
问题 I have a 512 x 512 x 112 matrix with three kind of values : zeroes, non zeroes and NaN. How can I get the indices of the non zero values of the matrix efficiently (without using loop)? 回答1: The comment from @scmg is the way to go- Matlab's linear logical indexing is a way to avoid looping over the elements; it only takes 1.2 sec on my PC. Here is a working example: rng(8675309) %jenny number for consistency x=randn([512,512,112]); % make random matrix x(x<0)=NaN; % set some elements to NaN x