Alternative for eval of dynamic variables

7 次查看(过去 30 天)
Hello,
From what I understand from the online community is that the use of dynamic variables is not acceptable/accurate/buggy.
I have 11 meshgrids (xyz-grids; to make it easy e.g. A,B,...,K) laying next to each other. For each of these 11 grids I want to calculate changing volumes over the years using 17 datasets (= xyz-data through the years). (the meshgrids have different sizes, that's why I did not make an overall grid)
I already assigned the xyz-data to my grids with scatteredInterpolant, so right now I have 17 variables (98,00,...,17) containing the same xy-values but different z-values (through the years; in total 11x17=189 variables; e.g. A_98,A_00,...,A17,B_98,...,K_17).
Now I need to calculate the volumes (=(difference between z-value & reference level)*gridsize; e.g. 50x50 and ref level -20) for each dataset for each mesh grid (so 17x11 times); afterwards I'll compare the results to each other so no need to worry about that.
The reason I like to use dynamic variables is the fact that results can be interpreted (more?) easily and that I don't really understand the online solutions for this.
Is there an efficient way to make a function or adjust my method/grids/variables/names to calculate the volumes of at least all datasets of 1 meshgrid (so 17x times) or even a way this can be done for all meshgrids at once (so 17x11 times)? (In the past I wrote everything 11x17 times which was very inefficient/dangerous for mistakes)
Thanks in advance for reading this (& maybe even helping me)!
  4 个评论
Stephen23
Stephen23 2020-4-4
编辑:Stephen23 2020-4-4
"Is there an efficient way..."
No.
You designed your data badly, which means you have forced yourself into writing inefficient code.
Bad data design cannot be fixed by patching it up afterwards via super-efficient magic commands that do not exist.
"The reason I like to use dynamic variables is the fact that results can be interpreted (more?) easily..."
Indexing is so simple and easy to understand that millions of programmers use indexing every day in countless different programming languages. Not using indexing is like cutting your feet off before running a race.
"In the past I wrote everything 11x17 times which was very inefficient/dangerous for mistakes"
Computers are really only good at one thing: doing simple tasks repeatedly in a loop. So whenever you copy-and-paste code like that you are just doing the computer's menial job for it (as well as increasing the risk of bugs, making code pointlessly complex, harder to maintain, etc.). The computer can repeat the task very efficiently when you use indexing.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2020-4-3
If you use a cell array you can store everything in a single array even if the size are different. Because you can index into the cell array you can use a simple for loop, removing any need to repeat your code.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by