Message Boards Message Boards

6 Replies
0 Total Likes
View groups...
Share this post:

to Select some elements of a matrix

Hi!, I have this: m ={{a, b, c}, {d, e, f}, {g, h, i}} which are the methods to create a list with one element of each groups of m. For exemple m1 = {a,e,i}, the first, the second and the third element?

I've try with m1 = {#[[1]]} & /@ m; but the selection is only the first element for each group!!


6 Replies

Thanks a lot for your suggestions! In fact with the Marco's command I can select the elements that i want! It is very very useful!

Length of what? You need more functions and the result is less flexible. In fact yours is a different suggestion. In my post above you pick an index that corresponds to an arbitrary element; you then apply two functions. You use two part functions with which you make into a pure function and then use MapIndexed. Using a different name for the list of entry choices (elements) there are fewer symbols in the first post than in yours as well.

More importantly, I suppose that the original poster wanted to be able to choose either random entries from each row, or elements that needed to be given in an additional vector. If the thing is just supposed to do what you suggest, why don't you simply use


Cheers, Marco

POSTED BY: Marco Thiel

Something like this:

RandomChoice /@ m

That gives you different elements every time you run it. If you want specific elements given in some list elements you can do this:

elements = {1, 2, 3};
MapThread[Part, {m, elements}]

Cheers, Marco

POSTED BY: Marco Thiel

Here's a slightly more concise version of Marco's answer that uses the index of Map's repetition instead of a manual list of elements:

m1=MapIndexed[#[[#2[[1]]]] &, m]

The #2[[1]] is getting the index in numerical form, because MapIndexed curiously returns it as a list of the form {1}.

POSTED BY: Jesse Friedman

More concise? By which metric?


POSTED BY: Marco Thiel

Length. It corresponds to the second piece in your post.

POSTED BY: Jesse Friedman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract