For example, I have 4 parameters: A, B, C, and D, each representing an observed property. Each stores a matrix with the size of 360 x 180.
Below is my loop:
Vars = ["A", "B", "C", "D"];
for i in Vars
if i == "A"
Z = A;
elseif i == "B"
Z = B;
elseif i == "C"
Z = C
elseif i == "D"
Z = D;
end
end
For my if loop, is it possible to archive that without writing a loop? After all, the name of the Matrix is equal to the name of the variable under each of the 4 conditions.
Consider using a different data structure? e.g. use a dictionary to store these parameters.
It’s hard to give sensible advice without more context, because the code you posted does exactly nothing — it would have no effect if you deleted the loop completely.
But depending on your actual problem, this may not be the best solution. without knowing this, @stevengj is probably right in saying, that a better data structure would lead to a better solution.
To answer Steven’s question, my use case is pretty simply. I just need to get the Z values depending on the variable name and then make a map plot. By storing my variables in a Dictionary, I can call them out based on the variable name (as a key) easily.