Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia problems with end, matrix

Tags:

julia

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

formMA

like image 292
Biljana Radovanovic Avatar asked Nov 18 '25 02:11

Biljana Radovanovic


1 Answers

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
like image 160
giordano Avatar answered Nov 19 '25 21:11

giordano