最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Reading and resaving in a file a matrix with the same name but with different dimensions with Mathematica - Stack Overflow

programmeradmin0浏览0评论

My main code runs a large program and saves the results in a text-like notebook (Put[...] & Save[...]) that contains result matrices, like e.g.,

tab1 = {{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}, {4, 8, 12, 16, 20, 24}}
tab10 = {{10, 20, 30, 40, 50, 60}, {20, 40, 60, 80, 100, 120}, {30, 60, 90, 120, 150, 180}, {40, 80, 120, 160, 200, 240}}

but of quite larger dimensions.

For the selective post-processing, I want to read specific files (Get[savefile]), load these tab1, tab10, tab# and then calculate "decreased resolution" versions of these matrices, e.g.,

smalltab1 = tab1[[Range[1, 4, 2], Range[1, 6, 2]]]
(*{{1,3,5},{3,9,15}}*)
smalltab10 = tab10[[Range[1, 4, 2], Range[1, 6, 2]]]
(*{{10,30,50},{30,90,150}}*)

Then, I want to rename these matrices as the original ones, e.g.,

tab1 = smalltab1
tab10 = smalltab10

(this is where the problem lies) and finally save the new file using Save[newsavefile,{tab1,tab10,tab#}] with the appropriate new filename.

Since the source files are large, I Get[...] them and "fish out" anything in them that is a result Matrix (not a standard number of them) and I end up with a list of these matrices, like tabs={"tab1","tab10",...}. I then create the decreased-resolution smaller matrices in a For loop with the common With process, like

For[t = 1, t <= Length[tabs], t++, 
    With[{lhs = Symbol["small" <> tabs[[t]]]}, lhs = Table[Symbol[tabs[[t]]][[i, k]], {i, 1, 4, 2}, {k, 1, 6, 2}]; 
];

This works fine without any problems. The next step would be to set tab1=smalltab1, tab10=smalltab10, ..., and this is where the problem is. If done directly in a cell as stated above, it works fine, but if it has to be done in a For loop using With

With[
  {lhs = Symbol[tabs[[i]]], rhs = Symbol["small" <> tabs[[i]]]}, 
  lhs = rhs
  ];

an error occurs because of the matrix dimensions mismatch (old tab1 with new tab1 etc.):

"Set::shape : Lists {...} and {...} are not of the same shape"

Also, trying to involve ClearAll, Remove etc. fails because the variables are local etc. Honestly, none of these is surprising. I know it is a memory address handling matter and the like, but how could I solve the problem?


After several days (weeks) of trying to solve the problem and failing, a friend was toying with a minimal working example and figured out a way to solve it. Although the solution is simple and includes things I had already tried, the specific command positioning and syntax is what precisely solves the problem. Here is the MWE and the solution:

tab1 = {{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 
    18}, {4, 8, 12, 16, 20, 24}};
tab10 = {{10, 20, 30, 40, 50, 60}, {20, 40, 60, 80, 100, 120}, {30, 
    60, 90, 120, 150, 180}, {40, 80, 120, 160, 200, 240}};
(*Drawn from external files. Tables number unknown and names \
unknown, but following the pattern tab#*)

tabs = {"tab1", "tab10"};
(*Fished out form the aforementioned external txt files and table \
created automatically.*)
ltabs = Length[tabs];

For[t = 1, t <= ltabs, t++,
  With[
    {lhs = Symbol["small" <> tabs[[t]]]}, 
    lhs = Symbol[tabs[[t]]][[Range[1, 4, 2], Range[1, 6, 2]]];
    ];
  ];
(*Example of the same statement and functions working fine.*)

Clear @@ tabs (*Clear all initial tables. THIS EXACT NEW STATEMENT \
IS WHAT SOLVES THE PROBLEM. The positioning and syntax are key.*)

For[t = 1, t <= ltabs, t++,
  With[
    {lhs = Symbol[tabs[[t]]], rhs = Symbol["small" <> tabs[[t]]]},
    lhs = rhs
    ];
  ];
(*Here is (*was*) the problem.*)

In[8]:= tab1

Out[8]= {{1, 3, 5}, {3, 9, 15}}

Further testing of this solution on the full scale code and external files will start today. Hopefully, the issue is solved.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论