slice

Dynamic slicing of Matlab array

。_饼干妹妹 提交于 2019-11-29 16:29:17
I have an n-dimensional array A and want to slice it dynamically, i.e., given a list of array dimensions, like [2 4], and a list of values, like [6 8], I want B = A(:,6,:,8,:,:,:,:,...) List lengths are unknown. Using eval would work but is not an option. This question is a generalization of a previous post to multiple indices and dimensions without a for-loop. rayryeng You can still use the previous post I linked to (which I originally flagged as a duplicate) to answer your question . This original post only slices in one dimension. I originally flagged it as a duplicate and closed it because

Using : for multiple slicing in list or numpy array

会有一股神秘感。 提交于 2019-11-29 16:24:17
I'm having some difficulty trying to figure out how to do extract multiple values in a list that are spaced some indices apart. For example, given a list l = [0,1,2,3,4,5,6,7,8,9,10] , I want to only extract the values [1,2,3] and [6,7,8,9] . I could do l[1:4]+l[6:-1] , but is there a way such to write l[1:4,6:-1] ? This is really a ghost problem to the actual problem I am having in a pandas dataframe. I have a dataframe, df , with columns ['A','B','C','I1','D','E','F','I2','I3'] , and I only want to keep the important columns ['I1', 'I2', 'I3'] . Now, the current approach I am doing is df

resize with averaging or rebin a numpy 2d array

℡╲_俬逩灬. 提交于 2019-11-29 16:08:57
问题 I am trying to reimplement in python an IDL function: http://star.pst.qub.ac.uk/idl/REBIN.html which downsizes by an integer factor a 2d array by averaging. For example: >>> a=np.arange(24).reshape((4,6)) >>> a array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]) I would like to resize it to (2,3) by taking the mean of the relevant samples, the expected output would be: >>> b = rebin(a, (2, 3)) >>> b array([[ 3.5, 5.5, 7.5], [ 15.5, 17.5, 19

Slicing sublists with different lengths

佐手、 提交于 2019-11-29 14:53:34
I have a list of lists. Each sublist has a length that varies between 1 and 100. Each sublist contains a particle ID at different times in a set of data. I would like to form lists of all particle IDs at a given time. To do this I could use something like: list = [[1,2,3,4,5],[2,6,7,8],[1,3,6,7,8]] list2 = [item[0] for item in list] list2 would contain the first elements of each sublist in list. I would like to do this operation not just for the first element, but for every element between 1 and 100. My problem is that element number 100 (or 66 or 77 or whatever) does not exists for every

What does Array.prototype.slice.call() & wrapper.querySelectorAll() do?

﹥>﹥吖頭↗ 提交于 2019-11-29 14:46:38
I found following cone in a js plugin var container = document.getElementById( 'vs-container' ), wrapper = container.querySelector( 'div.vs-wrapper' ), sections = Array.prototype.slice.call( wrapper.querySelectorAll( 'section' ) ), links = Array.prototype.slice.call( container.querySelectorAll( 'header.vs-header > ul.vs-nav > li' ) ); I couldn't understand what does Array.prototype.slice.call() & wrapper.querySelectorAll( 'section' ) do in above code. I've not seen them before so I would like to know what they actually do. querySelectorAll is a method on DOM elements that accepts a CSS

How to convert a slice into an array reference?

自古美人都是妖i 提交于 2019-11-29 14:32:44
I have an &[u8] and would like to turn it into an &[u8; 3] without copying. It should reference the original array. How can I do this? As of Rust 1.34, you can use TryFrom / TryInto : use std::convert::TryFrom; fn example(slice: &[u8]) { let array = <&[u8; 3]>::try_from(slice); println!("{:?}", array); } fn example_mut(slice: &mut [u8]) { let array = <&mut [u8; 3]>::try_from(slice); println!("{:?}", array); } They arrayref crate implements this. Here's an example, you can of course use it in different ways: #[macro_use] extern crate arrayref; /// Get the first 3 elements of `bytes` as a

What is the point of views in pandas if it is undefined whether an indexing operation returns a view or a copy?

ぃ、小莉子 提交于 2019-11-29 14:16:12
问题 I have switched from R to pandas. I routinely get SettingWithCopyWarnings, when I do something like df_a = pd.DataFrame({'col1': [1,2,3,4]}) # Filtering step, which may or may not return a view df_b = df_a[df_a['col1'] > 1] # Add a new column to df_b df_b['new_col'] = 2 * df_b['col1'] # SettingWithCopyWarning!! I think I understand the problem, though I'll gladly learn what I got wrong. In the given example, it is undefined whether df_b is a view on df_a or not. Thus, the effect of assigning

Split large Array in Array of two elements

我只是一个虾纸丫 提交于 2019-11-29 14:05:10
I have large list of objects and I need to split them in a group of two elements for UI propouse. Example: [0, 1, 2, 3, 4, 5, 6] Becomes an array with these four arrays [[0, 1], [2, 3], [4, 5], [6]] There are a ton of ways to split an array. But, what is the most efficient (least costly) if the array is huge. If you're looking for efficiency, you could have a method that would generate each array of 2 elements lazily, so you'd only store 2 elements at a time in memory: public struct ChunkGen<G : GeneratorType> : GeneratorType { private var g: G private let n: Int private var c: [G.Element]

Golang Join array interface

余生长醉 提交于 2019-11-29 13:56:01
I try to create bulk insert. I use gorm github.com/jinzhu/gorm import ( "fmt" dB "github.com/edwinlab/api/repositories" ) func Update() error { tx := dB.GetWriteDB().Begin() sqlStr := "INSERT INTO city(code, name) VALUES (?, ?),(?, ?)" vals := []interface{}{} vals = append(vals, "XX1", "Jakarta") vals = append(vals, "XX2", "Bandung") tx.Exec(sqlStr, vals) tx.Commit() return nil } But I got an error: Error 1136: Column count doesn't match value count at row 1 becuse i return wrong query INSERT INTO city(code, name) VALUES ('XX1','Jakarta','XX2','Bandung', %!v(MISSING)),(%!v(MISSING), %!v

How do I slice a string every 3 indices? [duplicate]

☆樱花仙子☆ 提交于 2019-11-29 12:39:49
问题 This question already has answers here : What is the most “pythonic” way to iterate over a list in chunks? (35 answers) Closed 4 years ago . I'm using Python to program for the lab I work at. How can I slice out every 3 characters in a given string and append it to a list? i.e. XXXxxxXXXxxxXXXxxxXXXxxxXXX (where X or x is any given letter) string = 'XXXxxxXXXxxxXXXxxxXXXxxxXXX' mylist = [] for x in string: string[?:?:?] mylist.append(string) I want the list to look like this: ['XXX','xxx',