1
$\begingroup$

I have {x, y, f(x,y)} data in multiple (n x 3) arrays with unequal length, which I read in from different files in order to later plot them using ListPlot3D. I wanted to automate the process and save the data into an indexed array. As the length is unequal, a standard 3D would not easily work (I would need to cut the zeros all the time for the plots).

I considered using variable names created by Symbol["x" <> ToString@i], but dealing with this is quite circumstantial.

I saw that people use indexed variables, so that is what I ended up with:

Do[x[k]=ReadList["data_"<>k<>".txt", {Number, Number, Number}], {k, 1, ndata}]

However, I learned that only symbols allow part assignments, so I e.g., cannot flip the sign using x[1][[All, 3]] *= -1. I could copy x[1] to an symbol, do the change there, and copy back, but that becomes circumstantial, again.

Is there a way around this?

Any thoughts how to better handle a list of unequal arrays is also appreciated.

Thank you!

$\endgroup$
3
  • 1
    $\begingroup$ Instead of assigning individual subsets of data to variables, assign the whole collection to one variable. You could do this with Table or several other strategies. Then x[[1,All,3]]*=-1 should work. $\endgroup$
    – lericr
    Commented Jul 11, 2023 at 13:10
  • $\begingroup$ Thanks! I was familiar with this route, but picked the I indexed variables because when I plot the x[[1]] using listplot3D, I have to delete all the zeros. But maybe that is less problematic. $\endgroup$
    – Jani
    Commented Jul 11, 2023 at 13:19
  • $\begingroup$ There shouldn't be any zeros. Using Table won't automatically pad with zeros. $\endgroup$
    – lericr
    Commented Jul 11, 2023 at 13:58

2 Answers 2

4
$\begingroup$

One solution that is quite close to your approach, is to use an Association instead of an indexed variable:

x = <||>;
Do[x[k]=ReadList["data_"<>k<>".txt", {Number, Number, Number}], {k, 1, ndata}]

Now you can do:

x[[Key[1], All, 3]] *= -1

If you use string keys, you can also get rid of the Key wrapper on the lhs.

$\endgroup$
1
$\begingroup$

Here is an example how you can store rows of different length in an anrray using e.g. Table:

d = Table[{a[i, j, 1], a[i, j, 1], a[i, j, 3]}, {i, 4}, {j, 
   RandomInteger[{2, 5}]}];
d // TableForm

enter image description here

$\endgroup$

Your Answer

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

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