to Select some elements of a matrix

Posted 9 years ago
3084 Views
|
6 Replies
|
0 Total Likes
|
 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!!thanks
6 Replies
Sort By:
Posted 9 years ago
 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!
Posted 9 years ago
 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 Diagonal[m] Cheers, Marco
Posted 9 years ago
 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 9 years ago
 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 9 years ago
 More concise? By which metric?M.
Posted 9 years ago
 Length. It corresponds to the second piece in your post.