From here I found that in a range construct one cannot find and replace its elements via array functions... How can be do it anyway?
Suppose I want to delete the elements 2,6,7,8,13,19 in range(1, step=1, stop=21). Or more generally, suppose a is a random array that contains numbers in the range [1,21] and one wants to delete these elementes in the given range.
You cannot delete from a range object, since that is immutable, but you can filter it:
julia> filter(x -> x ∉ [2,6,7,8,13,19], a)
15-element Array{Int64,1}:
1
3
4
5
9
10
11
12
14
15
16
17
18
20
21
However, if a is a "real" array, you can use filter! to operate in-place.
Another solution that if often convenient is to use InvertedIndices.jl package which exports Not and you can just use indexing:
julia> r = 1:21
1:21
julia> x = [2,6,7,8,13,19]
6-element Array{Int64,1}:
2
6
7
8
13
19
julia> r[Not(x)]
15-element Array{Int64,1}:
1
3
4
5
9
10
11
12
14
15
16
17
18
20
21
Related
powers = c(c(1:10), seq(from = 12, to=20, by=2));
While going through WGCNA i came across this code which i am not able to understand, can anybody explain me the meaning of that piece of code
The code will create a vector of numbers stored in powers.
Specifically: 1:10 creates the numbers 1 2 3 4 5 6 7 8 9 10 (can read as 1 through 10) and seq(from = 12, to = 20, by = 2) creates a sequence of every other number from 12 to 20, i.e. 12 14 16 18 20.
Powers will contain the following 15 numbers: 1 2 3 4 5 6 7 8 9 10 12 14 16 18 20
I am not familiar with the WGCNApackage or if powers is an argument to a function, but this is what powers contains.
I am trying to extract only the even numbers from the "cars" data set.
I know I need to create a new function.
I have come this far:
Is.even = function(x) x %% 2 == 0
When I enter in:
Is.even(cars[1])
It gives me back a logical response. I want to only display the actual even numbers in integer form and hide the odd numbers.
What am I doing wrong?
Apart from #neilfws' suggestion, if you pass your values as a vector you can also use Filter
Filter(Is.even, cars[, 1])
#[1] 4 4 8 10 10 10 12 12 12 12 14 14 14 14 16 16 18 18 18 18 20 20 20 20 20 22 24 24 24 24
I have an adjancecy matrix stored in CSR format. Eg
xadj = 0 2 5 8 11 13 16 20 24 28 31 33 36 39 42 44
adjncy = 1 5 0 2 6 1 3 7 2 4 8 3 9 0 6 10 1 5 7 11 2 6 8 12 3 7 9 13 4 8 14 5 11 6 10 12 7 11 13 8 12 14 9 13
I am now paritioning said graph using METIS. This gives me the partition vector part of the graph. Basically a list that tells me in which partition each vertex is. Is there an efficient way to build the new adjacency matrix for this partitioning such that I can partition the new graph again? Eg a function rebuildAdjacency(xadj, adjncy, part). If possible reusing xadj and adjncy.
I'm assuming that what you mean by "rebuild" is removing the edges between vertices that have been assigned different partitions? If so, the (probably) best you can do is iterate your CSR list, generate a new CSR list, and skip all edges that are between partitions.
In pseudocode (actually, more or less Python):
new_xadj = []
new_adjcy = []
for row in range(0, n):
row_index = xadj[row]
next_row_index = xadj[row+1]
# New row index for the row we are currently building
new_xadj.append(len(new_adjcy))
for col in adjncy[row_index:next_row_index]:
if partition[row] != partition[col]:
pass # Not in the same partition
else:
# Put the row->col edge into the new CSR list
new_adjcy.append(col)
# Last entry in the row index field is the number of entries
new_xadj.append(len(new_adjcy))
I don't think that you can do this very efficiently re-using the old xadj and adjcy fields. However, if you are doing this recursively, you can save memory allocation / deallocation by having exacyly two copies of xadj and adjc, and alternating between them.
I have a network (a directed acyclic graph):
dag_1 <- barabasi.game(20)
I applied a topological sort:
top1 <- topo_sort(dag_1)
top1
+ 20/20 vertices, from 0ee5d26:
[1] 5 8 11 13 14 15 16 17 18 20 4 7 12 19 2 10 9 6 3 1
If I type top1 and hit enter, the results are above. I need to access the vector
5 8 11 13, ..., 1
I tried top1[1] and top1[[1]]. Neither of them gave me the vector.
How can I get it?
top1 is an igraph.vs class object, and indexing e.g. top1[1:10] returns the vertices of the graph. To return a vector of the vertices use:
as.vector(top1)
My requirement is to select a window of size 5 from the 'data' variable and use it in further processing. (please see following code). However, the length of 'sub_data' increases for each iteration. What am I doing wrong?
next_one<-function(data) {
for(k in 10:length(data)) {
sub_data<-data[k-5:k];
print(sub_data);
}
}
I call the function as follows:
dat=read.csv("file name");
attach(dat);
#assume there is a column called 'Value'
next_one(Value);
Add parentheses:
(k-5):k
Compare
20-5:20
#[1] 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
with
(20-5):20
#[1] 15 16 17 18 19 20
And read help("Syntax") to learn about operator precedence.