Message Boards Message Boards

0
|
4493 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Replace part of a vector?

Posted 8 years ago

I want to replace elements 10-20 of a vector with 0's. I'm wondering why the following didn't work:

vect = Table[1, {100}]

Do[ReplacePart[vect, i->0], {i, 10, 20, 1}]

The resulting vector is still composed entirely of 1's. I'm using Mathematica 8.

POSTED BY: Herbert Swan
3 Replies

This one here is very direct:

Do[vect[[i]] = 0, {i, 10, 20}]

Cheers,

M.

POSTED BY: Marco Thiel
Posted 8 years ago

This also works

ReplacePart[vect, Table[i -> 0, {i, 10, 20}]]
POSTED BY: Okkes Dulgerci

Hi,

I think that this is because MMA does the substitution but you do not save that in the vector again. This works:

Do[vect = ReplacePart[vect, j -> 0], {j, 10, 20}]

Cheers,

M.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract