1
$\begingroup$

So, I want to create two vectors using a For.

Example:

Array[a, 5];
For[i = 1, i <= 5, i++, a[5] = i/5]
Array[b, 5];
For[i = 1, i <= 5, i++, b[5] = i/6]

And after that, I would like to plot the points {a[i], b[i]} and draw lines between them. I searched on the internet, but I couldn't find anything.

$\endgroup$

closed as off-topic by Michael E2, m_goldberg, Coolwater, Henrik Schumacher, LCarvalho Dec 23 '17 at 11:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, m_goldberg, Coolwater, Henrik Schumacher, LCarvalho
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Dec 23 '17 at 1:25
  • $\begingroup$ ListLinePlot[Table[{a[i], b[i]}, {i, 5}]]? $\endgroup$ – Michael E2 Dec 23 '17 at 1:25
2
$\begingroup$

You shouldn't need a For statement, while Table is far superior:

ListPlot[Table[{i/5, i/6}, {i, 5}], 
 Joined -> True, 
 PlotMarkers -> Green,
 PlotStyle -> Red]
$\endgroup$
  • 1
    $\begingroup$ If you want to be able to retrieve the a and b arrays later, you can set {a, b} = Transpose@Table[{i/5, i/6}, {i, 5}], so that, for example, b[[3]] == 1/2. For plotting the {a[i], b[i]} points from there, use ListPlot[Transpose@{a, b}, **options**]. $\endgroup$ – aardvark2012 Dec 22 '17 at 23:55
2
$\begingroup$

want to create two vectors using a For

(1) Assign values to a and b inside For:

For[i = 1, i <= 5, i++, a[i] = i/5]
For[i = 1, i <= 5, i++, b[i] = i/6]

(2a) Create data pairs directly using Array:

data = Array[{a @ #, b @ #}&, 5]

(2b) Alternatively, name two arrays, say aa and bb, then create pairs using Transpose:

{aa, bb} = Array[#, 5] & /@ {a, b};
data = Transpose[{aa, bb}];

(3) ListPlot data:

ListPlot[data, PlotStyle -> PointSize[Large], 
 Frame -> True, AspectRatio -> 1, PlotRange -> {{0, 1}, {0, 1}}, 
 PlotRangeClipping -> False, PlotRangePadding -> .1]

enter image description here

Note: You can also reverse the order of steps (1) and (2) to get the same result.

Update: For the case in comments:

But if I have a vector like this: For[ i=1, i<=n, i++, v[i] = Point[{i/2, i/3}]], how can I plot it?

n = 10;
For[i = 1, i <= n, i++, v[i] = Point[{i/2, i/3}]]
Graphics[{Red, PointSize@Large, Array[v, 10]}, Frame -> True, AspectRatio -> 1]

enter image description here

Alternatively, use v / @ Range[10] in place of Array[v, 10] to get the same result.

$\endgroup$
  • $\begingroup$ But if I have a vector like this: For[ i=1, i<=n, i++, v[i] = Point[{i/2, i/3}]], how can I plot it? $\endgroup$ – Princeton Dec 24 '17 at 20:44
  • $\begingroup$ @Princeton, please see the update. $\endgroup$ – kglr Dec 24 '17 at 20:58
  • $\begingroup$ And having the same type of vector, how can I draw lines between the points? I can't figure out a syntax for ListLinePlot if this is what is needed. $\endgroup$ – Princeton Dec 24 '17 at 21:16
  • $\begingroup$ Add Line[Join @@ List @@@ Array[v, 10]] after Array[v, 10] in Graphics. If you have to use ListLinePlot you can do ListLinePlot[Join @@ List @@@ Array[v, 10], Epilog -> Array[v, 10]]. $\endgroup$ – kglr Dec 24 '17 at 21:24

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