matrix

Converting multiple boolean columns to single factor column

守給你的承諾、 提交于 2020-01-09 11:51:57
问题 my data frame look like this: A S1 S2 S3 S4 1 ex1 1 0 0 0 2 ex2 0 1 0 0 3 ex3 0 0 1 0 4 ex4 1 0 0 0 5 ex5 0 0 0 1 6 ex6 0 1 0 0 7 ex7 1 0 0 0 8 ex8 0 1 0 0 9 ex9 0 0 1 0 10 ex10 1 0 0 0 i need to have it as a single factor list like: A Type ex1 S1 ex2 S2 ex3 S3 ex4 S1 ex5 S4 ex6 S2 ex7 S1 ex8 S2 ex9 S3 ex10 S1 anybody help my problem? 回答1: Assuming d is the data, the new column could be obtained with d$type <- names(d[-1])[apply(d[-1] == 1, 1, which)] d[c(1, 6)] # A type # 1 ex1 S1 # 2 ex2 S2

Why am I getting the error “Index exceeds matrix dimensions”?

隐身守侯 提交于 2020-01-09 11:43:00
问题 I am currently new to MATLAB. My code is below. I just have a question regarding why I keep getting the error "Index exceeds matrix dimensions" for the functions provided: a = [105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151 ... 175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167 ... 171 163 87 176 121 120 181 160 194 184 165 145 160 150 181 168 158 208 ... 133 135 172 171 237 170 180 167 176 158 156 229 158 148 150 118 143 141 ... 110 133 123 146 169

R - return position of element in matrix?

时光总嘲笑我的痴心妄想 提交于 2020-01-09 09:02:12
问题 Given a matrix: [,1] [,2] [1,] 0 0.0 [2,] -1 0.8 What is the quickest way in R to iterate over the matrix and return the position of all non-zero entries as an index? 回答1: Here is one approach mat = matrix(rnorm(9), 3, 3) which(mat !=0, arr.ind = T) 回答2: m <- matrix(c(0, 1, 1, 0), nrow = 2) which(m != 0) or maybe which(m != 0, TRUE) 来源: https://stackoverflow.com/questions/6522134/r-return-position-of-element-in-matrix

R - return position of element in matrix?

余生颓废 提交于 2020-01-09 09:01:46
问题 Given a matrix: [,1] [,2] [1,] 0 0.0 [2,] -1 0.8 What is the quickest way in R to iterate over the matrix and return the position of all non-zero entries as an index? 回答1: Here is one approach mat = matrix(rnorm(9), 3, 3) which(mat !=0, arr.ind = T) 回答2: m <- matrix(c(0, 1, 1, 0), nrow = 2) which(m != 0) or maybe which(m != 0, TRUE) 来源: https://stackoverflow.com/questions/6522134/r-return-position-of-element-in-matrix

NumPy array/matrix of mixed types

僤鯓⒐⒋嵵緔 提交于 2020-01-09 08:05:53
问题 I'm trying to create a NumPy array/matrix (Nx3) with mixed data types (string, integer, integer). But when I'm appending this matrix by adding some data, I get an error: TypeError: invalid type promotion . Please, can anybody help me to solve this problem? When I create an array with the sample data, NumPy casts all columns in the matrix to the one 'S' data type. And I can't specify data type for an array, because when i do this res = np.array(["TEXT", 1, 1], dtype='S, i4, i4') - I get an

Reading a Matrix txt file and storing as an array

痞子三分冷 提交于 2020-01-09 05:18:45
问题 I'm currently writing a Simulated Annealing code to solve a traveling salesman problem and have run into difficulties with storing and using my read data from a txt file. Each row & column in the file represents each city, with the distance between two different cities stored as a 15 x 15 matrix: 0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0 5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1 5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0 6.0 5.0 6.0 0.0 5.0

24. 蛇形填数

社会主义新天地 提交于 2020-01-07 20:59:02
题目: 在n × n 方阵里填入1,2,...,n × n。要求填成蛇形。例如,n = 4时方阵为: 10  11  12  1 9   16  13  2 8   15  14  3 7   6   5   4 上面的方阵中,多余的空格只是为了便于观察规律,不必严格输出。 n <= 8。 思路: 由题目可知,矩阵为方阵,因此用二维数组存储,同时将所有位置的值初始化为 0。 可将矩阵看成直角坐标系,并设每个数字坐标为(x , y),对应到二维数组的下标上。 假设当前位置为 “笔” 的位置,开始时,“笔” 在最右上角,坐标为(0 , n - 1) ,分析可得,“笔” 的移动轨迹为 先向下移动,再向左移动,再向上移动,再向右移动。整体上按照这4种方式,循环移动,必须注意,移动的先后次序不能颠倒。 举例来说,“笔” 一开始在最右上角,先填写数字,再向下移动,边移动边填写数字。 那么怎么判断该改变方向了呢?当向下移动时,先判断是否越过边界,再判断下一个位置是否被写过(没有被写过的格子值仍然是0, 被写过的格子值为数字),这样依次判断,直到 “笔” 应写的值超过格子数。 代码: #include <iostream>using namespace std;const int MAXN = 20;int matrix[MAXN][MAXN];int main(){ int n = 0; cin

数组索引 笔记2

霸气de小男生 提交于 2020-01-07 16:23:16
数组的类型及形状 numpy里面的数据类型 numpy里面的数据类型,其实就是封装了python里面的基础的数据类型,然后还进行细致划分,之后封装为np . 数据类型 1 ,创建数组的时候通过dtype属性来指定 元素的数据类型 arr = np . array ( [ 1 , 2 , 3 , 4 ] ) , dtype = float32 ( ) 2 ,也可以进行强制转换 res = np . float32 ( 5 ) 3 ,可以通过stype来修改数据类型 将arr的float32类型转化为int32类型 arr = arr . astype ( np . int32 ) arr . dtype = np . int32 - - > 转化类型正确,但是元素发生变化,不建议用 4 ,ndarray也可以存储复合数据类型 - - 了解 存储人对象 - - 人对象 3 个属性 - - 姓名,身高,体重 df = np . dtype ( [ ( 'name' , np . str , 40 ) , ( 'high' , np . float32 ) , ( 'weight' , np . float32 ) ] ) #创建一个存储复合数据的数组对象 arr = np . arrpy ( [ ( 'bq' , 168.5 , 50.5 ) , ( 'yf' , 172 , 55 )

I can't allocate memory for bigger matrices then ~200x200 Lapack Visual Studio in C Here is my code [duplicate]

久未见 提交于 2020-01-07 09:42:16
问题 This question already has answers here : What and where are the stack and heap? (25 answers) Closed 5 years ago . I am bench-marking the execution time of solving matrices , and i cant get more then ~200x200, i should go probably 1500x1500 or close to that. I am running this on VS . #include <stdlib.h> #include <stdio.h> #include "lapacke.h" #include "lapacke_config.h" #include <time.h> /* Auxiliary routines prototypes */ extern void print_matrix(lapack_complex_double *a, int m, int n);

How to perform PyCUDA 4x4 matrix inversion with same accuracy than numpy linalg “inv” or “pinv” function

試著忘記壹切 提交于 2020-01-07 09:36:00
问题 I am facing an issue of accuracy about my code which performs a number (128, 256, 512) of 4x4 matrix inversions. When I use the original version, i.e the numpy function np.linalg.inv or np.linalg.pinv , everything works fine. Unfortunately, with the CUDA code below, I get nan and inf values into inverted matrix. To be more explicit, I take this matrix to invert : 2.120771107884677649e+09 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 3