julia select all but one element in array/matrix

后端 未结 2 1331
萌比男神i
萌比男神i 2021-01-04 04:10

I would like to know if it is possible to select all but one element (by index) in a julia array.

For example in R language in order to not select a particular row i

2条回答
  •  孤街浪徒
    2021-01-04 04:39

    Here is one option:

    A = rand(3,3)
    B = A[1:end .!= 2,:]
    

    1:end gets a complete list of row indices (you could also use 1:size(A,1)) and then .!= (note the . indicating element-wise comparison) selects the indices not equal to 2.

    If you wanted to select columns in this way you would use:

    C = A[:, 1:end .!= 2]
    

    Note that the end keyword automatically will equal the last index value of the row, column, or other dimension you are referencing.

    Note: answer updated to reflect improvements (using end instead of size()) suggested by @Matt B in comments.

提交回复
热议问题