2
$\begingroup$

Is it possible to take elements of a matrix or list and use them as arguments of a function? I have a matrix of values, something like this;

  listed = {{3, 2, 1 , 5, 3, 2, 1}, {2, 3, 1, 4, 2, 1, 1}, {5, 1, 7, 5,  2, 1, 1}};

Now, if we take a function like Cylinder it requires something of the form;

 Graphics3D[Cylinder[{{x1,y1,z1},{x2,y2,z2}},t]]

with 7 arguments to specify it. What I'd like to do is take the rows of the columns and call these in, and do this for every row in the most automated fashion possible.In the example matrix I'd like Mathematica to render something like the image below -

enter image description here

This should be simple, but I'm running into problems. I've tried using forms of MapIndexed and Apply, but have had no joy and am open to suggestion on the best approach, as eventually I'll have to apply this so much bigger data sets than 7 x 3 !

$\endgroup$

2 Answers 2

5
$\begingroup$
Graphics3D[Cylinder[{{#1, #2, #3}, {#4, #5, #6}}, #7] & @@@ listed]

Or

Graphics3D[Cylinder[ArrayReshape[{##}, {2, 3}], #7] & @@@ listed]

Or

MapThread[Cylinder[ArrayReshape[{##}, {2, 3}], #7] &, Transpose@listed] // Graphics3D

Or

Graphics3D[Cylinder[ArrayReshape[#, {2, 3}], #[[-1]]] & /@ listed]
$\endgroup$
2
  • 1
    $\begingroup$ The last one is best if listed is packed as Map will only unpack the top level. $\endgroup$
    – rcollyer
    Mar 13, 2015 at 12:55
  • $\begingroup$ These are brilliant - thanks so much! I need to learn the nature of the @@@ handles a little better... $\endgroup$
    – DRG
    Mar 13, 2015 at 13:16
4
$\begingroup$
listed = {{3, 2, 1, 5, 3, 2, 1}, {2, 3, 1, 4, 2, 1, 1}, {5, 1, 7, 5, 
    2, 1, 1}};

Graphics3D[Cylinder[{#[[1 ;; 3]], #[[4 ;; 6]]}, #[[7]]] & /@ listed]

enter image description here

$\endgroup$

Your Answer

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

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