Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.1K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

Thread operator

Posted 10 years ago

Can somebody explain the following behaviour of "Thread" with multiple arguments: Consider:

t= {{a,b},{c,d}};
Thread[ t[[{1,2},{1,2}]] ]

I expected an answer of: {a,d}, but I get: {{a, c}, {b, d}} This looks like the outer product!

The same happens with the explicite form of Part:

Thread[Part[ t, {1,2}, {1,2}] ]

On the other hand, MapThread works as expected:

MapThread[  t[[##]] &, {{1, 2}, {1, 2}}  ]  

gives {a,d}

Have I misunderstood the manual or is this a bug?

POSTED BY: Daniel Huber
2 Replies
Posted 10 years ago

Thank you for you answer.

I learned from this, that care must be taken with "Thread", because Thread does not have the attribute "HoldFirst". This problem does not appear with MapThread because here the Arguments are separate.

POSTED BY: Daniel Huber

It's the order of evaluation. t[[{1, 2}, {1, 2}]] evaluates as List[{a,b},{c,d}], and Thread[List[{a,b},{c,d}]] gives {List[a,c],List[b,d]}. To get what you want you must reverse the order of evaluation, for example this way:

t = {{a, b}, {c, d}};
Thread[f[{1, 2}, {1, 2}]] /. f -> (t[[##]] &)
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard