Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.9K Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to use Select with conditions?

Posted 3 years ago

Hello, I am trying to use Select to select something meeting my requirements. For example:

Select[{{1, 2, 3}, {4, 1, 6}}, Abs[#3 - #2] > 3 &]

I hope this can select lists in which Abs[#3-#2]>3, such as {4,1,6}. But I see the code above does not work rightly. Where did I wrong and what is the right code?

POSTED BY: Zhenyu Zeng
4 Replies
Posted 3 years ago

You could also use Cases

Cases[{{1, 2, 3}, {4, 1, 6}}, {_, x_, y_} /; Abs[y - x] > 3]
POSTED BY: Rohit Namjoshi
Posted 3 years ago

Good way. Cases is more vivid. Thanks.

POSTED BY: Zhenyu Zeng

You need to use Part in this case

Select[{{1, 2, 3}, {4, 1, 6}}, Abs[#[[3]] - #[[2]]] > 3 &]
POSTED BY: Ahmed Elbanna
Posted 3 years ago

Yes, I just can't image this way myself. When you pointed it out, I can understand. Thanks.

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