array

How do I calculate all pairs of vector differences in numpy?

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know I can do np.subtract.outer(x, x) . If x has shape (n,) , then I end up with an array with shape (n, n) . However, I have an x with shape (n, 3) . I want to output something with shape (n, n, 3) . How do I do this? Maybe np.einsum ? 回答1: You can use broadcasting after extending the dimensions with None / np.newaxis to form a 3D array version of x and subtracting the original 2D array version from it, like so - x[:, np.newaxis, :] - x Sample run - In [6]: x Out[6]: array([[6, 5, 3], [4, 3, 5], [0, 6, 7], [8, 4, 1]]) In [7]: x[:,None,:]

Converting CSV File Into 2D Array

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an example of my idea in a 1d array. It will only output the columns. My idea is to use a 2d array to select the row and column. Here my code: String fName = "c:\\csv\\myfile.csv"; String thisLine; int count=0; FileInputStream fis = new FileInputStream(fName); DataInputStream myInput = new DataInputStream(fis); int i=0; while ((thisLine = myInput.readLine()) != null) { String strar[] = thisLine.split(";"); out.println(strar[1]); // Here column 2 } myfile.csv Id;name E1;Tim A1;Tom Output: name Tim Tom 回答1: I would just add the split

Copy an array reference in VBA

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there any way to copy an array reference in VBA (or VB6)? In VBA, arrays are value types. Assigning one array variable to another copies the entire array. I want to get two array variables to point to the same array. Is there any way to accomplish this, perhaps using some API memory functions and/or the VarPtr function, which does in fact return the address of a variable in VBA? Dim arr1(), arr2(), ref1 As LongPtr arr1 = Array("A", "B", "C") ' Now I want to make arr2 refer to the same array object as arr1 ' If this was C#, simply assign,

Shared Array not shared correctly in python multiprocessing

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am experimenting multiprocessing in Python and tried to share an Array of strings among two processes. Here is my python code : from multiprocessing import Process, Array, Value import ctypes def f1(a, v): for i, l in enumerate(['a', 'b', 'c']): a[i] = l*3 v.value += 1 print "f1 : ", a[:], v.value def f2(a,v): v.value += 1 print "f2 : ", a[:], v.value if __name__ == '__main__': val = Value(ctypes.c_int, 0) arr = Array(ctypes.c_char_p, 3) print "Before :", arr[:], val.value p = Process(target=f1, args=(arr, val)) p2 = Process(target=f2,

Dynamically add a private property to an object

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a class: class Foo { // Accept an assoc array and appends its indexes to the object as property public function extend($values){ foreach($values as $var=>$value){ if(!isset($this->$var)) $this->$var = $value; } } } $Foo = new Foo; $Foo->extend(array('name' => 'Bee')); Now the $Foo object has a public name property with value Bee . How to change extend function to make variables private ? Edit Using a private array is another way and definitely not my answer. 回答1: Just plain, bad design. What's the purpose of adding a private [!] field

Ruby inner flatten (array of arrays)

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an array like the following [ [[0, :a], [2, :b]], [3, :c], [4, :d], [[5, :e], [6, :f], [7, :g]] ] That is, an Array of elements that are either (1) 2-element Arrays, or (2) an Array of 2-element Arrays. I am trying to find an elegant way to "flatten" this array such that elements that are (2) get expanded out into root-level elements. In this example: [[0, :a], [2, :b], [3, :c], [4, :d], [5, :e], [6, :f], [7, :g]] This is almost like using Array#flatten(depth) , except depth needs to work from the inside out, rather than the outside

How to declare an array with an arbitrary size

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Ok, this is a C programming homework question. But I'm truly stuck. I ask the user to input words, and then I insert the input into an array, but I can't have any control over the number of words the user types. I guess what I'm asking is how do you declare a an array in C without declaring its length and without asking the user what the length should be. I know this has something to do with malloc, but if you could give me some examples of how to do this, I would really appreciate it. 回答1: You can realloc it every time like: int size = 0;

How do numpy's in-place operations (e.g. `+=`) work?

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The basic question is: What happens under the hood when doing: a[i] += b ? Given the following: import numpy as np a = np.arange(4) i = a > 0 i = array([False, True, True, True], dtype=bool) I understand that: a[i] = x is the same as a.__setitem__(i, x) , which assigns directly to the items indicated by i a += x is the same as a.__iadd__(x) , which does the addition in place But what happens when I do : a[i] += x Specifically: Is this the same as a[i] = a[i] + x ? (which is not an in-place operation) Does it make a difference in this case if

Swift error comparing two arrays of optionals

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I get a compilation error in the next Swift code var x:Array<Int?> = [1,2] var y:Array<Int?> = [1,2] if x == y { // Error } If both arrays are Array<Int> it works fine, but if at least one of them is optional it throws an error like the next: Binary operator '==' cannot be applied to two Array<Int?> operands I filed a bug report months ago but I had no answer. It still occurs in Swift 1.2. Why is this happening? 回答1: The issue here is the distinction between something having an == operator, versus something being “equatable”. Both Optional

split string into array of n words per index

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a string that I'd like to split in an array that has (for example) 3 words per index. What I'd also like it to do is if it encounters a new line character in that string that it will "skip" the 3 words limit and put that in a new index and start adding words in that new index until it reaches 3 again. example var text = "this is some text that I'm typing here \n yes I really am" var array = text.split(magic) array == ["this is some", "text that I'm", "typing here", "yes I really", "am"] I've tried looking into regular expressions, but