0
jj=RandomReal[{-1,1},1000];

a=c;  (* c is any constant *)

For[k=1,k<=1000,k++,
y={{a^2*jj[k],a*jj[k]},{a*jj[k],(a^3+4)*jj[k]}};
   ];

x1=Sort[y[[1,1]]]; x2=Sort[y[[2,2]]];

p1=ListLinePlot[{x1,x2}]

I want to plot x1 vs. x2. I know they can be computed even without using a matrix as here but I would like to know how elements of a matrix can be manipulated either for calculations or for plotting. I would like to thank you in advance.

user31694
  • 189
  • 6
  • 5
    Please describe what you are trying to do in plain words, in such a way that it is understandable without any code. – Szabolcs Sep 11 '17 at 08:01
  • 1
    You do realize that the first line is irrelevant since j is overridden immediately in the For loop? Also, the loop just overrides yover and over. And finally, the Table statement in the last line also does nothing interesting, since nothing depends on j. (look at x1/x2) – Lukas Lang Sep 11 '17 at 08:05
  • @Mathe172 In the first line I am generating 1000 random numbers which are used in the For loop.You are right about Table as I mentioned. – user31694 Sep 11 '17 at 08:47
  • perhaps For[n = 1, n <= 1000, n++, y = {{a^2*j[[n]], a*j[[n]]}, {a*j[[n]], (a^3 + 4)*j[[n]]}};]; – Sumit Sep 11 '17 at 09:43
  • "1000 random numbers which are used in the For loop" - no, they are not. In the loop, j is an iterator that goes from 1 to 1000. Period. Your random numbers, as defined in the first line, are j[[1]] as the first element, j[[2]] as the second, ..., j[[i]] as the ith. Maybe you want something like: For[i=1,i<=1000,i++, y={{a^2*j[[i]],a*j[[i]]},{a*j[[i]],(a^3+4)*j[[i]]}}; ]? But note that y is just {{0.0867359 a^2, 0.0867359 a}, {0.0867359 a, 0.0867359 (4 + a^3)}}... If you want a list of such ys applied to each random number j[[i]], just do: (....) – corey979 Sep 11 '17 at 09:47
  • (....) Table[{{a^2 j[[i]], a j[[i]]}, {a j[[i]], (a^3 + 4) j[[i]]}}, {i, 1, 1000}] or simply {{a^2 #, a #}, {a #, (a^3 + 4) #}} & /@ j. See also Why should I avoid the For loop in Mathematica? – corey979 Sep 11 '17 at 09:48
  • @corey979 Now, I edited my question, please have a look. This is a simple example code I have written. I need these techniques in much bigger context. – user31694 Sep 11 '17 at 10:30
  • @user31694 Did you read through the answer in the linked post? There's still no reason to use For in your example, even jj*{{a^2,a},{a,a^3+4}} would work here since every element is simply multiplied by jj[k]. And to repeat: You just assign a new value to y in every iteration of the loop in your current code – Lukas Lang Sep 11 '17 at 11:14
  • @Mathe172 It is true for this particular example but I have a much general problem at hand which needs such multiplication inside a matrix. – user31694 Sep 11 '17 at 11:24
  • 4
    I voted to close this question as unclear because you refused to explain the problem in simple words, and people are clearly wasting their time by trying to guess. The code has so many problems that it is impossible to tell what you are trying to do. Remember that even if the question gets closed, it can be re-opened after it has been edited and the problem has been explained in a clear manner. – Szabolcs Sep 11 '17 at 11:38
  • All you need to do is explain your problem in words (so it can be understood even without reference to any code). Why do you keep changing the code? – Szabolcs Sep 11 '17 at 21:50

1 Answers1

1

This is a bit of a shot in the dark, as your actual goal is a little opaque.

To figure things out, start with a smaller list:

SeedRandom[1];
jj = RandomReal[{-1, 1}, 10];

Notice that

Clear[a]
a^2 jj

(* {0.634779 a^2, -0.777161 a^2, 0.579052 a^2, -0.624394 a^2, -0.517278 a^2,   
   -0.868522 a^2, 0.0844932 a^2, -0.537691 a^2, -0.207988 a^2, 0.400948 a^2} *)

That is, if you want to multiply a list by a number, you can just multiply a list by a number. So, perhaps you want:

y1 = Transpose[{a^2 jj, a jj}];
y2 = Transpose[{a jj, (a^3 + 4) jj}];

(* y1 = {{0.634779 a^2, 0.634779 a}, {-0.777161 a^2, -0.777161 a}, {0.579052 a^2, 0.579052 a}, 
         {-0.624394 a^2, -0.624394 a}, {-0.517278 a^2, -0.517278 a}, {-0.868522 a^2, -0.868522 a}, 
         {0.0844932 a^2, 0.0844932 a}, {-0.537691 a^2, -0.537691 a}, {-0.207988 a^2, -0.207988 a}, 
         {0.400948 a^2, 0.400948 a}} *)

So y1 and y2 are lists of pairs generated from your random numbers by the specified formulae.

I will assume you want to plot these points for some value of a:

ListPlot[{y1, y2} /. a -> 0.5]

enter image description here

There are more concise ways of doing this, if you so wish. I would strongly encourage you to thoroughly read the documentation of any interesting looking functions and play with the examples therein. Most things are probably much easier than you think they'll be.

aardvark2012
  • 5,424
  • 1
  • 11
  • 22