When I type this error jumps in julia but I don't know why, it should be working./
julia> A = [1 2 3 4; 5 6 7 8; 1 2 3 4; 5 6 7 8]
4×4 Array{Int64,2}:
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
julia> B = A[2:1:end; 2:1:end]
ERROR: syntax: missing last argument in "2:1:" range expression
Stacktrace:
[1] top-level scope at REPL[9]:0
The syntax to index a multidimensional array uses a comma , instead of semicolon ; as separator between dimensions, see https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1. Thus you want to do:
julia> A = [1 2 3 4; 5 6 7 8; 1 2 3 4; 5 6 7 8]
4×4 Array{Int64,2}:
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
julia> B = A[2:1:end, 2:1:end]
3×3 Array{Int64,2}:
6 7 8
2 3 4
6 7 8
Note also that you can omit 1 in the range specification, as step 1 is the default:
julia> A[2:end, 2:end]
3×3 Array{Int64,2}:
6 7 8
2 3 4
6 7 8
Related
I have a vector and a matrix (Array{T,1} and Array{T,2}) in my Julia code and I would like to append them such that the vector becomes a new row in the matrix (should be first row). I've tried several methods (cat, etc.) but keep getting errors which I believe are related to the different shape of the data. See the example below.
julia> v = Vector([1, 2, 3])
3-element Array{Int64,1}:
1
2
3
julia> m = Matrix([4 5 6; 7 8 9])
2×3 Array{Int64,2}:
4 5 6
7 8 9
julia> cat(v,m,dims=(1,2))
5×4 Array{Int64,2}:
1 0 0 0
2 0 0 0
3 0 0 0
0 4 5 6
0 7 8 9
What I actually want is
1 2 3
4 5 6
7 8 9
I realize that I can get this to work with transpose(v) but I was hoping to avoid extra calls.
Thanks!
As long as you can change the construction of v to a 1 x 3 array, you can avoid the transpose:
julia> v = [1 2 3]
1×3 Array{Int64,2}:
1 2 3
julia> m = [4 5 6; 7 8 9]
2×3 Array{Int64,2}:
4 5 6
7 8 9
julia> vcat(v, m)
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
I think that just doing the transpose
julia> v2 = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3
julia> vcat(v2', m)
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
is almost as efficient though.
How can I delete a specific row in Julia? Let's say I have an array:
[A , 2
B , 4
C , 6]
I want to delete the lines for which 'B' is in the first column. I can identify which row this is, but am not able to delete this row. Can anybody help me?
Thanks,
Nico
julia> a = rand(1:10, 5,3)
5×3 Array{Int64,2}:
4 5 7
8 4 3
8 6 3
10 4 1
9 3 10
To delete row 4:
julia> row = 4
julia> a = a[setdiff(1:end, row), :]
4×3 Array{Int64,2}:
4 5 7
8 4 3
8 6 3
9 3 10
Say you have a dataframe called "data".
julia> data=DataFrame(rand(1:10, 5,3))
5×3 DataFrames.DataFrame
Row x1 x2 x3
1 9 1 1
2 8 5 8
3 9 2 2
4 9 6 5
5 3 8 7
You want to delete entire row where column x1 has value 8.
julia> data[data[:x1].!=8,:]
4×3 DataFrames.DataFrame
Row x1 x2 x3
1 9 1 1
2 9 2 2
3 9 6 5
4 3 8 7
I have two matrices A and B of dimension 5 by 3 and 5 by 2, respectively. I want to produce series of matrices combining each column of matrix B to A. The dimensions of the resulting matrices would be 5 by 4
Let A be
1 2 3
4 5 6
7 8 9
2 3 1
4 1 5
and B be
1 2
2 5
3 8
6 3
2 1
Then the resulting matrices are
1 2 3 1
4 5 6 2
7 8 9 3
2 3 1 6
4 1 5 2
and
1 2 3 2
4 5 6 5
7 8 9 8
2 3 1 3
4 1 5 1
Use our old friend the assignment operator. Assigning 1st column of B to 4th of A:
A[, 4] <- B[, 1]
> A
V1 V2 V3 V4
1 1 2 3 1
2 4 5 6 2
3 7 8 9 3
4 2 3 1 6
5 4 1 5 2
Then A[, 4] <- B[, 2], etc.
I have a data frame and a function. I when I run a function with the data frame as an argument, somehow it seems it changes the structure.
So I am getting errors.
I pasted the code and the data frame I am working on.
The error I am getting is "Error in plot[row, 1] : incorrect number of dimensions"
Code
avg.value = function(plot,delta,row) {
#plot=deparse(substitute(plot))
#delta=Week1DeltaT
value=as.numeric()
avg.value=as.numeric()
for (row in 1:11) {
# compute firtst value separately
value[1]=plot[row,1]*delta[1]
value[1]
# Loop for rest of the values
i=1
end = length(delta)-1
end
for( i in 1:end) {
value[i+1]=(plot[row,i+1]-plot[row,i])*delta[i+1]
i=i+1
}
avg.value[row]=sum(value)/4
}
return(avg.value)
}
Variable(data frame)
plot
6 7 8 9 10 11 12 13
R1 3 3 4 4 4 4 4 4
R2 1 3 3 3 3 4 4 4
R3 1 1 3 4 4 4 4 4
R4 1 3 4 4 4 4 4 4
R5 3 4 4 4 4 4 4 4
R6 1 3 3 4 4 4 4 4
R7 1 2 2 3 3 4 4 4
R8 2 4 4 4 4 4 4 4
R9 1 2 2 4 4 4 4 4
R10 3 4 4 4 4 4 4 4
R11 1 1 4 4 4 4 4 4
delta(a numeric vector of length 8)
6 7 8 9 10 11 12 13
row(a single numeric value. It can be from 1 to 11)
1~11, numeric
I realized from the comment that I accidentally begin the loop from the wrong place. Now this function works as I wanted.
I have this code which gives the number of occurrences of each element of w vector in vector y
a= [7 4 9 6 4 10 9 6 7 6]
y=zeros(size(a));
for i=1:length(a)
y(i)=sum(a==a(i));
end
y = y;
end
y=[2 2 2 3 2 1 2 3 2 3]
but this result is not enough because I need to know the index along with the number of repartitions. The result should look like:
element no of repartitions position1 position2 position3
7 2 1 9 0
4 2 2 5 0
9 2 3 7 0
6 3 4 8 10
10 1 0 0 0