I want to make a matrix from 1D Arrays in Julia
I am new to Julia and need some help.
I have a list of 1D Arrays, I want to produce a matrix like this
g = [ 2.0 -1.0 0.0 0.0;
-1.0 2.0 -1.0 0.0;
0.0 -1.0 2.0 -1.0;
0.0 0.0 -1.0 3.0]
I am able to follow simple examples given on docs, but how do I achieve this using loops?
Cheers,
Ayush
julia
add a comment |
I am new to Julia and need some help.
I have a list of 1D Arrays, I want to produce a matrix like this
g = [ 2.0 -1.0 0.0 0.0;
-1.0 2.0 -1.0 0.0;
0.0 -1.0 2.0 -1.0;
0.0 0.0 -1.0 3.0]
I am able to follow simple examples given on docs, but how do I achieve this using loops?
Cheers,
Ayush
julia
2
Possible duplicate of How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?
– Colin T Bowers
Nov 28 '18 at 5:58
add a comment |
I am new to Julia and need some help.
I have a list of 1D Arrays, I want to produce a matrix like this
g = [ 2.0 -1.0 0.0 0.0;
-1.0 2.0 -1.0 0.0;
0.0 -1.0 2.0 -1.0;
0.0 0.0 -1.0 3.0]
I am able to follow simple examples given on docs, but how do I achieve this using loops?
Cheers,
Ayush
julia
I am new to Julia and need some help.
I have a list of 1D Arrays, I want to produce a matrix like this
g = [ 2.0 -1.0 0.0 0.0;
-1.0 2.0 -1.0 0.0;
0.0 -1.0 2.0 -1.0;
0.0 0.0 -1.0 3.0]
I am able to follow simple examples given on docs, but how do I achieve this using loops?
Cheers,
Ayush
julia
julia
asked Nov 28 '18 at 5:18
Ayush PatnaikAyush Patnaik
1
1
2
Possible duplicate of How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?
– Colin T Bowers
Nov 28 '18 at 5:58
add a comment |
2
Possible duplicate of How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?
– Colin T Bowers
Nov 28 '18 at 5:58
2
2
Possible duplicate of How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?
– Colin T Bowers
Nov 28 '18 at 5:58
Possible duplicate of How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?
– Colin T Bowers
Nov 28 '18 at 5:58
add a comment |
1 Answer
1
active
oldest
votes
with loops you can create a matrix and then fill it.
with loops
horizontally:
julia> arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0],[1.,1.,1.,1.]]
5-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
[1.0, 1.0, 1.0, 1.0]
julia> function make_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input[1]), length(input)
for col in input
(height == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:width
output[:,i] = input[i]
end
return output
end
make_matrix (generic function with 1 method)
julia> make_matrix(arr)
4×5 Array{Float64,2}:
2.0 -1.0 0.0 0.0 1.0
-1.0 2.0 -1.0 0.0 1.0
0.0 -1.0 2.0 -1.0 1.0
0.0 0.0 -1.0 3.0 1.0
vertically:
julia> function vmake_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input),length(input[1])
for col in input
(width == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:height
output[i,:] = input[i]
end
return output
end
vmake_matrix (generic function with 1 method)
julia> vmake_matrix(arr)
5×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
1.0 1.0 1.0 1.0
without loops
without loops you can use vcat
or hcat
depending on the direction you want to concatenate the arrays in.
julia> H_arr = [ [ 2.0 -1.0 0.0 0.0],[-1.0 2.0 -1.0 0.0],[0.0 -1.0 2.0 -1.0],[0.0 0.0 -1.0 3.0] ]
4-element Array{Array{Float64,2},1}:
[2.0 -1.0 0.0 0.0]
[-1.0 2.0 -1.0 0.0]
[0.0 -1.0 2.0 -1.0]
[0.0 0.0 -1.0 3.0]
julia> vcat(H_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
julia> V_arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0]]
4-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
julia> hcat(V_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512614%2fi-want-to-make-a-matrix-from-1d-arrays-in-julia%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
with loops you can create a matrix and then fill it.
with loops
horizontally:
julia> arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0],[1.,1.,1.,1.]]
5-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
[1.0, 1.0, 1.0, 1.0]
julia> function make_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input[1]), length(input)
for col in input
(height == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:width
output[:,i] = input[i]
end
return output
end
make_matrix (generic function with 1 method)
julia> make_matrix(arr)
4×5 Array{Float64,2}:
2.0 -1.0 0.0 0.0 1.0
-1.0 2.0 -1.0 0.0 1.0
0.0 -1.0 2.0 -1.0 1.0
0.0 0.0 -1.0 3.0 1.0
vertically:
julia> function vmake_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input),length(input[1])
for col in input
(width == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:height
output[i,:] = input[i]
end
return output
end
vmake_matrix (generic function with 1 method)
julia> vmake_matrix(arr)
5×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
1.0 1.0 1.0 1.0
without loops
without loops you can use vcat
or hcat
depending on the direction you want to concatenate the arrays in.
julia> H_arr = [ [ 2.0 -1.0 0.0 0.0],[-1.0 2.0 -1.0 0.0],[0.0 -1.0 2.0 -1.0],[0.0 0.0 -1.0 3.0] ]
4-element Array{Array{Float64,2},1}:
[2.0 -1.0 0.0 0.0]
[-1.0 2.0 -1.0 0.0]
[0.0 -1.0 2.0 -1.0]
[0.0 0.0 -1.0 3.0]
julia> vcat(H_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
julia> V_arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0]]
4-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
julia> hcat(V_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
add a comment |
with loops you can create a matrix and then fill it.
with loops
horizontally:
julia> arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0],[1.,1.,1.,1.]]
5-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
[1.0, 1.0, 1.0, 1.0]
julia> function make_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input[1]), length(input)
for col in input
(height == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:width
output[:,i] = input[i]
end
return output
end
make_matrix (generic function with 1 method)
julia> make_matrix(arr)
4×5 Array{Float64,2}:
2.0 -1.0 0.0 0.0 1.0
-1.0 2.0 -1.0 0.0 1.0
0.0 -1.0 2.0 -1.0 1.0
0.0 0.0 -1.0 3.0 1.0
vertically:
julia> function vmake_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input),length(input[1])
for col in input
(width == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:height
output[i,:] = input[i]
end
return output
end
vmake_matrix (generic function with 1 method)
julia> vmake_matrix(arr)
5×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
1.0 1.0 1.0 1.0
without loops
without loops you can use vcat
or hcat
depending on the direction you want to concatenate the arrays in.
julia> H_arr = [ [ 2.0 -1.0 0.0 0.0],[-1.0 2.0 -1.0 0.0],[0.0 -1.0 2.0 -1.0],[0.0 0.0 -1.0 3.0] ]
4-element Array{Array{Float64,2},1}:
[2.0 -1.0 0.0 0.0]
[-1.0 2.0 -1.0 0.0]
[0.0 -1.0 2.0 -1.0]
[0.0 0.0 -1.0 3.0]
julia> vcat(H_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
julia> V_arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0]]
4-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
julia> hcat(V_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
add a comment |
with loops you can create a matrix and then fill it.
with loops
horizontally:
julia> arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0],[1.,1.,1.,1.]]
5-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
[1.0, 1.0, 1.0, 1.0]
julia> function make_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input[1]), length(input)
for col in input
(height == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:width
output[:,i] = input[i]
end
return output
end
make_matrix (generic function with 1 method)
julia> make_matrix(arr)
4×5 Array{Float64,2}:
2.0 -1.0 0.0 0.0 1.0
-1.0 2.0 -1.0 0.0 1.0
0.0 -1.0 2.0 -1.0 1.0
0.0 0.0 -1.0 3.0 1.0
vertically:
julia> function vmake_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input),length(input[1])
for col in input
(width == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:height
output[i,:] = input[i]
end
return output
end
vmake_matrix (generic function with 1 method)
julia> vmake_matrix(arr)
5×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
1.0 1.0 1.0 1.0
without loops
without loops you can use vcat
or hcat
depending on the direction you want to concatenate the arrays in.
julia> H_arr = [ [ 2.0 -1.0 0.0 0.0],[-1.0 2.0 -1.0 0.0],[0.0 -1.0 2.0 -1.0],[0.0 0.0 -1.0 3.0] ]
4-element Array{Array{Float64,2},1}:
[2.0 -1.0 0.0 0.0]
[-1.0 2.0 -1.0 0.0]
[0.0 -1.0 2.0 -1.0]
[0.0 0.0 -1.0 3.0]
julia> vcat(H_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
julia> V_arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0]]
4-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
julia> hcat(V_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
with loops you can create a matrix and then fill it.
with loops
horizontally:
julia> arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0],[1.,1.,1.,1.]]
5-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
[1.0, 1.0, 1.0, 1.0]
julia> function make_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input[1]), length(input)
for col in input
(height == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:width
output[:,i] = input[i]
end
return output
end
make_matrix (generic function with 1 method)
julia> make_matrix(arr)
4×5 Array{Float64,2}:
2.0 -1.0 0.0 0.0 1.0
-1.0 2.0 -1.0 0.0 1.0
0.0 -1.0 2.0 -1.0 1.0
0.0 0.0 -1.0 3.0 1.0
vertically:
julia> function vmake_matrix(input::Vector{<:Vector})
element_type = eltype(eltype(input))
if (length(input) == 0)
return Array{element_type,2}(undef,0,0)
end
height,width = length(input),length(input[1])
for col in input
(width == length(col)) ? nothing : throw("inconsistent array size")
end
output = Array{element_type}(undef,height,width)
for i in 1:height
output[i,:] = input[i]
end
return output
end
vmake_matrix (generic function with 1 method)
julia> vmake_matrix(arr)
5×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
1.0 1.0 1.0 1.0
without loops
without loops you can use vcat
or hcat
depending on the direction you want to concatenate the arrays in.
julia> H_arr = [ [ 2.0 -1.0 0.0 0.0],[-1.0 2.0 -1.0 0.0],[0.0 -1.0 2.0 -1.0],[0.0 0.0 -1.0 3.0] ]
4-element Array{Array{Float64,2},1}:
[2.0 -1.0 0.0 0.0]
[-1.0 2.0 -1.0 0.0]
[0.0 -1.0 2.0 -1.0]
[0.0 0.0 -1.0 3.0]
julia> vcat(H_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
julia> V_arr = [[2.0,-1.0,0.0,0.0],[-1.0,2.0,-1.0,0.0],[0.0,-1.0,2.0,-1.0],[0.0,0.0,-1.0,3.0]]
4-element Array{Array{Float64,1},1}:
[2.0, -1.0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 0.0]
[0.0, -1.0, 2.0, -1.0]
[0.0, 0.0, -1.0, 3.0]
julia> hcat(V_arr...)
4×4 Array{Float64,2}:
2.0 -1.0 0.0 0.0
-1.0 2.0 -1.0 0.0
0.0 -1.0 2.0 -1.0
0.0 0.0 -1.0 3.0
edited Nov 28 '18 at 17:26
answered Nov 28 '18 at 16:54
WarrenWarren
1486
1486
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512614%2fi-want-to-make-a-matrix-from-1d-arrays-in-julia%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of How to convert Array{Array{Float64, 1}, 1} to Matrix in julia?
– Colin T Bowers
Nov 28 '18 at 5:58