1
$\begingroup$

I have a large list of lists with the dimensions {441,21,21,3} of which I need to print the x,y,z data from every 21st part up to 441. How can I print the 21x21x3 piece of every 21st part of this list? Here's what I've tried below that did not do the job. Also is there an more memory efficient way to perform this task?:

Table[Print["mylist parts", mylist[[i++21]]], {i, 1, 441}];
$\endgroup$
1
  • 1
    $\begingroup$ Are you sure i++21 is what you meant to write? $\endgroup$
    – Szabolcs
    May 5, 2012 at 12:10

3 Answers 3

2
$\begingroup$

You can select every 21st element using

mylist[[21;; ;; 21]]

You can print them using

Do[Print[mylist[[i]]], {i, 21, 441, 21}]

Regarding memory, having these displayed in the Front End will take more memory than just having the data in kernel memory, so Print /@ mylist[[21;; ;; 21]] may be better. Why do you want to print them? If you're aim is to write to a file, there are other ways.

EDIT

To number the parts, you can use Do[Print[Style[i/21, Large]; Print[mylist[[i]]], {i, 21, 441, 21}]

$\endgroup$
4
  • $\begingroup$ Just for a check at present to make sure I'm getting all the right pieces. I agree this is a memory strain and may not be possible all at one time. Thanks for your help! $\endgroup$ May 5, 2012 at 12:16
  • $\begingroup$ This works very well! Is there a way to number the parts as their printed? Thanks so much! $\endgroup$ May 5, 2012 at 12:21
  • $\begingroup$ @RHall Does my edit to the answer help? $\endgroup$
    – Szabolcs
    May 5, 2012 at 12:43
  • $\begingroup$ Perfect! Thanks! $\endgroup$ May 5, 2012 at 13:30
3
$\begingroup$

An alternative is Take

For example:

mat = Array[Subscript[a, ##] &, {50, 3}];

Take[mat, {1, -1, 5}]
$\endgroup$
2
$\begingroup$

Here is another way you can select every 21st element of your list.

sel= Table[mylist[[i]],{i,1,Length@mylist,21}];

Because Mathmatica is interactive, you can then examine one, many or all the selected elements that are held in sel by using expressions like:

sel[[1]]        (* first element *)

sel[[7;;11]]    (* elements 7 through 11 *)

sel[[{2,6,14}]] (* elements 2,6,14 *)

sel[[10;;-2]]   (* every element from 10 until the second last element *)

This should let you examine what you have selected whenever you wish without having to use Print;

$\endgroup$
1
  • $\begingroup$ Fantastic! Thanks very much! $\endgroup$ May 5, 2012 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.