Julia: 2d array assignment
I'm trying to do the assignment in the 2d array with the nested loop. I'm trying to access the elements of the array as follows. But I get a mistake. I've searched, but I didn't get results. How can I assign Julia in the 2d array?
for x in 1:total
for y in 1:W
@show (x, y)
if agirliklar[x] <= y
V[x][y] = getMax(V[x-1][y], degerler[x] + V[x-1][y - agirliklar[x]])
else
print("sa")
V[x][y] = V[x-1][y]
end
end
end
BoundsError: attempt to access 7×6 Array{Int64,2} at index [0]
My code
Error
arrays multidimensional-array julia-lang
add a comment |
I'm trying to do the assignment in the 2d array with the nested loop. I'm trying to access the elements of the array as follows. But I get a mistake. I've searched, but I didn't get results. How can I assign Julia in the 2d array?
for x in 1:total
for y in 1:W
@show (x, y)
if agirliklar[x] <= y
V[x][y] = getMax(V[x-1][y], degerler[x] + V[x-1][y - agirliklar[x]])
else
print("sa")
V[x][y] = V[x-1][y]
end
end
end
BoundsError: attempt to access 7×6 Array{Int64,2} at index [0]
My code
Error
arrays multidimensional-array julia-lang
It would be useful to see (example) definitions oftotal
,W
,agirliklar
,degerler
, andV
. But maybe it's just thatx-1
is one-off?
– phg
Dec 30 '18 at 10:06
add a comment |
I'm trying to do the assignment in the 2d array with the nested loop. I'm trying to access the elements of the array as follows. But I get a mistake. I've searched, but I didn't get results. How can I assign Julia in the 2d array?
for x in 1:total
for y in 1:W
@show (x, y)
if agirliklar[x] <= y
V[x][y] = getMax(V[x-1][y], degerler[x] + V[x-1][y - agirliklar[x]])
else
print("sa")
V[x][y] = V[x-1][y]
end
end
end
BoundsError: attempt to access 7×6 Array{Int64,2} at index [0]
My code
Error
arrays multidimensional-array julia-lang
I'm trying to do the assignment in the 2d array with the nested loop. I'm trying to access the elements of the array as follows. But I get a mistake. I've searched, but I didn't get results. How can I assign Julia in the 2d array?
for x in 1:total
for y in 1:W
@show (x, y)
if agirliklar[x] <= y
V[x][y] = getMax(V[x-1][y], degerler[x] + V[x-1][y - agirliklar[x]])
else
print("sa")
V[x][y] = V[x-1][y]
end
end
end
BoundsError: attempt to access 7×6 Array{Int64,2} at index [0]
My code
Error
arrays multidimensional-array julia-lang
arrays multidimensional-array julia-lang
asked Dec 30 '18 at 8:43
Mehmet AcarMehmet Acar
1
1
It would be useful to see (example) definitions oftotal
,W
,agirliklar
,degerler
, andV
. But maybe it's just thatx-1
is one-off?
– phg
Dec 30 '18 at 10:06
add a comment |
It would be useful to see (example) definitions oftotal
,W
,agirliklar
,degerler
, andV
. But maybe it's just thatx-1
is one-off?
– phg
Dec 30 '18 at 10:06
It would be useful to see (example) definitions of
total
, W
, agirliklar
, degerler
, and V
. But maybe it's just that x-1
is one-off?– phg
Dec 30 '18 at 10:06
It would be useful to see (example) definitions of
total
, W
, agirliklar
, degerler
, and V
. But maybe it's just that x-1
is one-off?– phg
Dec 30 '18 at 10:06
add a comment |
2 Answers
2
active
oldest
votes
In Julia arrays are 1-based not 0-based.
You try to access V[x-1]
where x
can take value of 1
.
Site note: always provide a minimum working example (MWE) rather than just dumping a part of your production code.
add a comment |
(At least) two things are wrong here:
- As @PrzemyslawSzufel says, ordinary Julia arrays are 1-indexed, so you cannot access them at index zero. Though it is possible to get special arrays that are 0-indexed.
- If
V
is a 2D array, as you are saying, you cannot access it like this:V[x][y]
. Instead you access them like this:V[x, y]
. You can read more about this here: https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1
Thanks it works
– Mehmet Acar
Jan 1 at 6:58
If one of the answers solves your problem, please consider marking one of them as the accepted answer.
– DNF
Jan 1 at 10:44
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%2f53976269%2fjulia-2d-array-assignment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Julia arrays are 1-based not 0-based.
You try to access V[x-1]
where x
can take value of 1
.
Site note: always provide a minimum working example (MWE) rather than just dumping a part of your production code.
add a comment |
In Julia arrays are 1-based not 0-based.
You try to access V[x-1]
where x
can take value of 1
.
Site note: always provide a minimum working example (MWE) rather than just dumping a part of your production code.
add a comment |
In Julia arrays are 1-based not 0-based.
You try to access V[x-1]
where x
can take value of 1
.
Site note: always provide a minimum working example (MWE) rather than just dumping a part of your production code.
In Julia arrays are 1-based not 0-based.
You try to access V[x-1]
where x
can take value of 1
.
Site note: always provide a minimum working example (MWE) rather than just dumping a part of your production code.
answered Dec 30 '18 at 11:15
Przemyslaw SzufelPrzemyslaw Szufel
1,719111
1,719111
add a comment |
add a comment |
(At least) two things are wrong here:
- As @PrzemyslawSzufel says, ordinary Julia arrays are 1-indexed, so you cannot access them at index zero. Though it is possible to get special arrays that are 0-indexed.
- If
V
is a 2D array, as you are saying, you cannot access it like this:V[x][y]
. Instead you access them like this:V[x, y]
. You can read more about this here: https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1
Thanks it works
– Mehmet Acar
Jan 1 at 6:58
If one of the answers solves your problem, please consider marking one of them as the accepted answer.
– DNF
Jan 1 at 10:44
add a comment |
(At least) two things are wrong here:
- As @PrzemyslawSzufel says, ordinary Julia arrays are 1-indexed, so you cannot access them at index zero. Though it is possible to get special arrays that are 0-indexed.
- If
V
is a 2D array, as you are saying, you cannot access it like this:V[x][y]
. Instead you access them like this:V[x, y]
. You can read more about this here: https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1
Thanks it works
– Mehmet Acar
Jan 1 at 6:58
If one of the answers solves your problem, please consider marking one of them as the accepted answer.
– DNF
Jan 1 at 10:44
add a comment |
(At least) two things are wrong here:
- As @PrzemyslawSzufel says, ordinary Julia arrays are 1-indexed, so you cannot access them at index zero. Though it is possible to get special arrays that are 0-indexed.
- If
V
is a 2D array, as you are saying, you cannot access it like this:V[x][y]
. Instead you access them like this:V[x, y]
. You can read more about this here: https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1
(At least) two things are wrong here:
- As @PrzemyslawSzufel says, ordinary Julia arrays are 1-indexed, so you cannot access them at index zero. Though it is possible to get special arrays that are 0-indexed.
- If
V
is a 2D array, as you are saying, you cannot access it like this:V[x][y]
. Instead you access them like this:V[x, y]
. You can read more about this here: https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1
answered Dec 31 '18 at 10:03
DNFDNF
2,430619
2,430619
Thanks it works
– Mehmet Acar
Jan 1 at 6:58
If one of the answers solves your problem, please consider marking one of them as the accepted answer.
– DNF
Jan 1 at 10:44
add a comment |
Thanks it works
– Mehmet Acar
Jan 1 at 6:58
If one of the answers solves your problem, please consider marking one of them as the accepted answer.
– DNF
Jan 1 at 10:44
Thanks it works
– Mehmet Acar
Jan 1 at 6:58
Thanks it works
– Mehmet Acar
Jan 1 at 6:58
If one of the answers solves your problem, please consider marking one of them as the accepted answer.
– DNF
Jan 1 at 10:44
If one of the answers solves your problem, please consider marking one of them as the accepted answer.
– DNF
Jan 1 at 10:44
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%2f53976269%2fjulia-2d-array-assignment%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
It would be useful to see (example) definitions of
total
,W
,agirliklar
,degerler
, andV
. But maybe it's just thatx-1
is one-off?– phg
Dec 30 '18 at 10:06