I need to make a sequence of an array with different length by reading a dataset. I need to call each of them in a loop so probably I need some sort of indexing in order to call them. For example, how can I create the following sequence:
P[1]=[1 2 3 4]
P[2]=[1 4]
P[3]=[8 9 0 0 5 6]
.
.
.
Here it is:
julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])
3-element Array{Array{Int64,1},1}:
[1, 2, 3, 4]
[1, 4]
[8, 9, 0, 0, 5, 6]
julia> P[1]
4-element Array{Int64,1}:
1
2
3
4
julia> P[2]
2-element Array{Int64,1}:
1
4
julia> P[3]
6-element Array{Int64,1}:
8
9
0
0
5
6
If you want to add a new element use push!():
julia> push!(P,[7,8,9])
4-element Array{Array{Int64,1},1}:
[1, 2, 3, 4]
[1, 4]
[8, 9, 0, 0, 5, 6]
[7, 8, 9]
Related
Sorry for the newbie question so i have an array as code below:
import numpy as np
p = np.array([[2,3,0,5],[2,3,4,5],[2,3,4,5],[0,0,0,0]])
p[np.where(p[0]==0)]=100
print(p)
I wanted to change the first rows 0th value to be 100. However the output is:
[[ 2 3 0 5]
[ 2 3 4 5]
[100 100 100 100]
[ 0 0 0 0]]
So it was changing the 3rd row. A bit perplex. Can I use where? What are other suggestions.
Kevin
[[2 3 100 5]
[2 3 4 5]
[2 3 4 5]
[0 0 0 0]]
Directly use indexing:
p[0, p[0]==0] = 100
Updated p:
array([[ 2, 3, 100, 5],
[ 2, 3, 4, 5],
[ 2, 3, 4, 5],
[ 0, 0, 0, 0]])
I have an array with the shape (10000,6). For example:
a = np.array([[5, 5, 5, 5, 5, 5][10, 10, 10, 10, 10][15, 15, 15, 15, 15]...])
I want to take every 25th array and subtract its element values from the next 25 elements until a new subtraction array in selected. so for example if the first array is:
[10, 10, 10, 10, 10]
then these values should be subtracted on the array itself and the next 25 arrays until for example a new subtraction array like this is selected:
[2, 2, 2, 2, 2]
then the array itself and the following 25 elements should be subtracted that arrays values.
This means that after the operation every 25th array will be:
[0, 0, 0, 0, 0]
because it has been subtracted by itself.
Here's what I would do:
import numpy as np
arr = np.random.randint(0, 10, (9, 3))
group_size = 3
# select vectors you want ot subtract and copy them {group_size} times
selected = arr[::group_size].repeat(3, axis = 0)
# subtract selected vectors from all vectors in the group
sub_arr = arr-selected
output:
arr =
[[9 6 3]
[8 3 3]
[2 0 4]
[0 3 9]
[3 9 9]
[0 8 6]
[4 0 0]
[6 1 9]
[2 6 4]]
selected =
[[9 6 3]
[9 6 3]
[9 6 3]
[0 3 9]
[0 3 9]
[0 3 9]
[4 0 0]
[4 0 0]
[4 0 0]]
sub_arr =
[[ 0 0 0]
[-1 -3 0]
[-7 -6 1]
[ 0 0 0]
[ 3 6 0]
[ 0 5 -3]
[ 0 0 0]
[ 2 1 9]
[-2 6 4]]
You can reshape your array so that each chunk has the right number of lines, and then simply subtract the first line
import numpy as np
a = np.arange(10000)[:, None] * np.ones(6)
a = a.reshape(-1, 25, 6)
a -= a[:, 0, :][:, None, :]
a = a.reshape(-1, 6)
i want to make a list by combining the results of n. but when the x values are different, it creates a new list. However, if the x value is the same, it will be in 1 list
My code is like this
muy = [[1,2,3],[4,5,6],[7,8,9]]
# will = []
for x in muy:
for y in muy:
if x != y:
print(x, " ", y)
m = np.subtract(x, y)
n = sum(m)
print(m)
print(n)
the result is like this
[1, 2, 3] [4, 5, 6]
[-3 -3 -3]
-9
[1, 2, 3] [7, 8, 9]
[-6 -6 -6]
-18
[4, 5, 6] [1, 2, 3]
[3 3 3]
9
[4, 5, 6] [7, 8, 9]
[-3 -3 -3]
-9
[7, 8, 9] [1, 2, 3]
[6 6 6]
18
[7, 8, 9] [4, 5, 6]
[3 3 3]
9
the result what i want is like this:
[[-9,-18][9,-9][18,9]]
what should i do?
muy = [[1,2,3],[4,5,6],[7,8,9]]
will = []
for x in muy:
temp = []
for y in muy:
if x != y:
m = np.subtract(x, y)
n = sum(m)
temp.append(n)
will.append(temp)
print(will)
I'm a bit puzzled about the way that Julia 1.0.3 treats global variables. Is there a way to use !push to update a global array?
While playing in the REPL, I want to update a global variable, then push! the result to a global array to store it.
var = [1]
res = []
for i in 1:5
global var
global res
push!(var,i)
print(string(var,"\n"))
push!(res,var)
end
However, the values stored in res are as follows:
[1, 1, 2, 3, 4, 5]
[1, 1, 2, 3, 4, 5]
[1, 1, 2, 3, 4, 5]
[1, 1, 2, 3, 4, 5]
[1, 1, 2, 3, 4, 5]
Whereas I would expect this:
[1, 1]
[1, 1, 2]
[1, 1, 2, 3]
[1, 1, 2, 3, 4]
[1, 1, 2, 3, 4, 5]
Particularly puzzling since behaviour seems as expected with variables, instead of arrays:
var = 1
res = []
for i in 1:5
global var
global res
var = var + i
print(string(var,"\n"))
push!(res, var)
end
Which gives expected result:
2
4
7
11
16
I am clearly missing something.
You're pushing the same var array to every spot in the res array. For example:
julia> var = [1]
1-element Array{Int64,1}:
1
julia> res = [var, var]
2-element Array{Array{Int64,1},1}:
[1]
[1]
julia> var[1] = 2
2
julia> res
2-element Array{Array{Int64,1},1}:
[2]
[2]
Both elements in the res array are var itself. So if you modify var (with push! or indexed assignment or somesuch), then no matter how you access it you'll see those modifications.
This doesn't occur with numbers because you cannot modify numbers themselves. You can change which number is stored in an array, but you cannot change the number 1 to represent 2 everywhere that 1 had previously been used — that's the equivalent of what's happening here.
To fix this, you'll often want to just create your var array inside the for loop (instead of outside it). But in this case, since you're iteratively adding things to var and want to save that intermediate state, you can use copy:
julia> for i in 1:5
global var
global res
push!(var,i)
print(string(var,"\n"))
push!(res,copy(var))
end
Any[1]
Any[1, 2]
Any[1, 2, 3]
Any[1, 2, 3, 4]
Any[1, 2, 3, 4, 5]
julia> res
5-element Array{Any,1}:
Any[1]
Any[1, 2]
Any[1, 2, 3]
Any[1, 2, 3, 4]
Any[1, 2, 3, 4, 5]
I need to copy a part of a 3D array.
I have the indexes of start and end of the copy.
For example 2D array:
[[2 2 3 4 5]
[2 3 3 4 5]
[2 3 4 4 5]
[2 3 4 5 5]
[2 3 4 5 6]]
starting index, end index are:
mini = [2, 1]
maxi = [4, 3]
So the result should be:
[[3 4 4]
[3 4 5]]
I can write:
result = matrix[mini[0]:maxi[0], mini[1]:maxi[1]]
Is there a way to do it generally ? for 3Dim or NDim arrays ?
The trick here is realizing what the indexing syntax is under the hood. This:
result = matrix[mini[0]:maxi[0], mini[1]:maxi[1]]
Is shorthand in python (not just numpy) for:
indices = slice(mini[0], maxi[0]), slice(mini[1], maxi[1])
result = matrix[indices]
So we just need to generate indices dynamically:
lower = [2, 1, ...]
upper = [4, 3, ...]
indices = tuple(np.s_[l:u] for l, u in zip(lower, upper))
result = matrix_nd[indices]
np.s_[a:b] is a shorthand for slice(a, b). Here we build a tuple containing as many slices as you have values in lower and upper
What you are looking for is the slice object, see that example:
matrix = np.random.rand(4,5)
mini = [2, 1]
maxi = [4, 3]
slices=[slice(b,e) for b, e in zip(mini,maxi)]
print(slices)
print(matrix[slices])
print(matrix[mini[0]:maxi[0], mini[1]:maxi[1]])