Group Abstract Group Abstract

Message Boards Message Boards

1
|
2.3K Views
|
2 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Use Replace and Part at the same time?

Posted 2 years ago

Hello,

a = {6, 5, 4, 3, 2, 
  1}; {{1, 2, 3}, {4, 5, 6}} /. {d_, b_, c_} -> {a[[d]], a[[b]], a[[c]]}

would get errors:

Part::pkspec1: The expression d cannot be used as a part specification. Part::pkspec1: The expression b cannot be used as a part specification. Part::pkspec1: The expression c cannot be used as a part specification. General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.

Is there a way to use them at the same time?

Thanks.

POSTED BY: Zhenyu Zeng
2 Replies
Posted 2 years ago

Try this:

a = {6, 5, 4, 3, 2, 1};

{{1, 2, 3}, {4, 5, 6}} /. x_Integer :> a[[x]]

Or

{{1, 2, 3}, {4, 5, 6}} /. {d_, b_, c_} :> {a[[d]], a[[b]], a[[c]]}
POSTED BY: Hans Milton
Posted 2 years ago

Thanks. This works. I don't why it would work.

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