Message Boards Message Boards

0
|
6334 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Save a vector constructed inside a loop?

Posted 4 years ago

Hello everyone. I am working in a problem in which a for loop with k iterations is present. Inside of said loop, a vector of length l is constructed and displayed on the screen. I, however, need to store those k vectors. I have attempted to construct a matrix of zeros of size k*l outside the loop as

ShannonSavedX = ConstantArray[0, {l, k}];

And then, inside of the for loop, using the letter i as index I try to store the vector in question inside of the matrix as

ShannonSavedX[[All, i]] = shannonIPEPSX

being shannonIPEPSX the name of the vector in question. If I write

Dimensions[shannonIPEPS]

The output is {121}, the value of l in this case. If I write (before the loop)

Dimensions[ShannonSavedX]

The output is {121, 3}. However, if I try to plot the results after, they don't coincide with the screen output, which is the correct one. The output are nine identical rows, each one containing what should be each of the columns of ShannonSavedX.

Can someone please help me with this? Thanks.

This is the saved matrix is

This is what the saved matrix should be

POSTED BY: Jaime de la Mota
5 Replies
Posted 4 years ago

Don't use a For loop, use Table.

saved = Table[RandomInteger[i, 5], {i, 1, 5}]

Replace RandomInteger with whatever calculates the vectors.

POSTED BY: Rohit Namjoshi

Thanks for yout kind answers. Hoever, I am afraid that my loop is complex. This is how my code looks:

For[i = 3, i < countMax + 3, i++,

 Xpos = aaa[[All, 1]]; Ypos = aaa[[All, 2]]; 
 windXVal = aaa[[All, i]]; 
 windXMat = Transpose[{Xpos, Ypos, windXVal}]; 
 Print[ListContourPlot[windXMat //. {x_List} :> x, 
   PlotLegends -> Automatic]]; 
 ifuncEPSX = Interpolation[ windXMat //. {x_List} :> x]; 
 Print[ContourPlot[ifuncEPSX[t, u], {t, -75, 0}, {u, 30, 65}, 
   GridLines -> {{xmin, xmax}, None}, PlotLegends -> Automatic]]; 
 intDataVectorEPSX = 
  Flatten[Table[{t, u, ifuncEPSX[t, u]}, {t, xmin, xmax, dDeltaX}, {u,
      ymin, ymax, dDeltaY}]]; leEPS = Length@intDataVectorEPSX; 
 vectorXintEPS = Table[intDataVectorEPSX[[i]], {i, 1, leEPS - 2, 3}]; 
 vectorYintEPS = Table[intDataVectorEPSX[[i]], {i, 2, leEPS - 1, 3}]; 
 vectorFintEPSX = Table[intDataVectorEPSX[[i]], {i, 3, leEPS, 3}]; 
 interpolatedDataEPSX = 
  Transpose[{vectorXintEPS, vectorYintEPS, vectorFintEPSX}]; 
 shannonIPEPS[t_, u_] = 
  Total[#3* sinc[(t - #1)/dDeltaX]*sinc[(u - #2)/dDeltaY] & @@@ 
    interpolatedDataEPSX];
 Print[shannonIPEPS[t_, u_]]; 
 Print[ContourPlot[
   shannonIPEPS[t, u], {t, xmin, xmax}, {u, ymin, ymax}, 
   GridLines -> {{xmin, xmax}, None}, PlotLegends -> Automatic]]; 
 ShannonSavedX[[All, i - 2]] = shannonIPEPS[t_, u_]]

Since I am working with such a complex operation I don't know where to use the table. Can you please indicate me how to do it?

Thanks again. Jaime.

POSTED BY: Jaime de la Mota
Posted 4 years ago

See if you can adapt this

saved=Table[
  Xpos = aaa[[All, 1]]; Ypos = aaa[[All, 2]]; windXVal = aaa[[All, i]]; 
  windXMat = Transpose[{Xpos, Ypos, windXVal}]; 
  ifuncEPSX = Interpolation[ windXMat //. {x_List} :> x]; 
  intDataVectorEPSX = Flatten[Table[{t, u, ifuncEPSX[t, u]}, {t, xmin, xmax, dDeltaX}, {u,ymin, ymax, dDeltaY}]];
  leEPS = Length@intDataVectorEPSX; 
  vectorXintEPS = Table[intDataVectorEPSX[[i]], {i, 1, leEPS - 2, 3}]; 
  vectorYintEPS = Table[intDataVectorEPSX[[i]], {i, 2, leEPS - 1, 3}]; 
  vectorFintEPSX = Table[intDataVectorEPSX[[i]], {i, 3, leEPS, 3}]; 
  interpolatedDataEPSX = Transpose[{vectorXintEPS, vectorYintEPS, vectorFintEPSX}]; 
  Total[#3* sinc[(t - #1)/dDeltaX]*sinc[(u - #2)/dDeltaY] & @@@ interpolatedDataEPSX],
  {i,3,countMax+2}]
POSTED BY: Bill Nelson

Thank you so much. The output is just what I need. I will try to understand what you have written. Thank you so much.

Regards.

Jaime.

POSTED BY: Jaime de la Mota

You haven't shown all your code. The usual method for doing this kind of problem is to use Table to evaluate the expression and construct a matrix of the results. Loops are rarely an effective way to get a job done in Mathematica.

POSTED BY: John Doty
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract