Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to plot a lot of arrows in a sphere (3D-Plot). That is not a problem with a small number of Arrows; for a few I can do than something like this:

Show[
  Graphics3D[{Thickness[Large], Arrowheads[Large],
    Arrow[{{0, 0, 0}, {1, 0, 0}}], Arrow[{{0, 0, 0}, {0, 1, 0}}]}, 
    Axes -> True], 
  SphericalPlot3D[1, {θ, 0, Pi}, {φ, 0, 2*Pi}, 
    PlotStyle -> Opacity[0.2]]]

However, I want do something like a for-loop to plot many Arrows

For[i = 1, i < 11, i++, Arrow[{{0, 0, 0}, {1/i, (1-i)/5, (1-i)/5}}]

I have tried to paste for loop in many places of my graphics code, but without success. How should I build that code?

share|improve this question
3  
For loops do not create arrays. They repeat an action several times. Try using Table instead. –  Searke Mar 4 '13 at 16:47
add comment

1 Answer 1

up vote 5 down vote accepted

Use Table when you want to create a list of values.

myarrows = Table[Arrow[{{0, 0, 0}, {1/i, (1 - i)/5, (1 - i)/5}}], {i, 1, 10}]

Graphics3D[myarrows]

For loops only repeat an evaluation over and over - it doesn't put the result of the evaluations into a list.

share|improve this answer
1  
One might put an Append[] in the middle of the loop, or even use Sow[] and Reap[] in connection with a loop, in order to generate a list. However, there are few scenarios in which either of these is better than using Table[] as you suggest. –  Michael Stern Mar 4 '13 at 22:36
    
Sow and Reap hadn't occurred to me an a quick solution here until just now actually. I feel bad that I don't use it pr functions like it more often. –  Searke Mar 5 '13 at 16:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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