How to simply this without writing a loop?

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.

Many thanks!

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.

3 Likes

Thanks, Steven!

The goal of my if loop is to assign the respective matrix to Z.

Perhaps something like:

julia> A=[1 2 ; 3 4]
2×2 Matrix{Int64}:
 1  2
 3  4

julia> s="A"
"A"

julia> Z=eval(Symbol(s))
2×2 Matrix{Int64}:
 1  2
 3  4
1 Like

It works! :grinning:

That’s exactly what I’m looking for. Thank you so much.

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.

2 Likes

I’ll try to design my system using Dict instead, but it is always nice to learn of a new trick.

1 Like

I’d say most of the time if you have to resort to eval you’re doing it wrong and should step back to reconsider.

7 Likes

I’ll go out on a limb and say that in > 99% of cases, using eval with global variables is not the best solution.

The goal of my if loop is to assign the respective matrix to Z.

This is not what I meant by “more context”. I wanted you to explain why you think you need to so something like this.

4 Likes

Maybe something like this is what sevengj meant, which quite clean and avoids the eval:

julia> A = rand(10,10); B = rand(10,10); C = rand(10,10); D = rand(10,10);

julia> vars = Dict("A" => A, "B" => B, "C" => C, "D" => D)
Dict{String, Matrix{Float64}} with 4 entries:
  "B" => [0.504199 0.170964 … 0.367923 0.658817; 0.881139 0.116055 … 0.178827 0.624661; … ; 0.296164 0.0850091 … 0.142666 0.651318; 0.…
  "A" => [0.809202 0.513267 … 0.487085 0.138561; 0.366652 0.379529 … 0.104835 0.0992703; … ; 0.439847 0.570735 … 0.644324 0.200732; 0.…
  "C" => [0.884361 0.078709 … 0.944589 0.268905; 0.888668 0.723977 … 0.957192 0.61467; … ; 0.473355 0.823675 … 0.0485727 0.312304; 0.1…
  "D" => [0.153201 0.970533 … 0.768051 0.652922; 0.219684 0.135139 … 0.193671 0.335816; … ; 0.473319 0.460035 … 0.746215 0.807457; 0.9…

julia> Z = vars["A"]
10×10 Matrix{Float64}:
 0.809202  0.513267  0.26274    0.57556    0.0991993  0.17318   0.975175  0.226455   0.487085  0.138561
 0.366652  0.379529  0.908708   0.936167   0.771327   0.976005  0.582927  0.928687   0.104835  0.0992703
 0.393059  0.669549  0.606634   0.113105   0.456589   0.875129  0.787877  0.397731   0.571865  0.554807
 0.561105  0.638307  0.537919   0.800292   0.442311   0.114075  0.717245  0.631823   0.404449  0.590554
 0.953813  0.957735  0.0580233  0.0492704  0.176854   0.20227   0.991575  0.0915628  0.761263  0.941246
 0.361479  0.657399  0.182579   0.281445   0.638095   0.751553  0.306348  0.184897   0.32337   0.912145
 0.715396  0.324467  0.671328   0.826675   0.368116   0.68645   0.670547  0.70813    0.959706  0.0967608
 0.1752    0.199804  0.252097   0.850838   0.269474   0.861059  0.70255   0.968217   0.953311  0.694385
 0.439847  0.570735  0.447643   0.38973    0.431662   0.486043  0.712614  0.232222   0.644324  0.200732
 0.53924   0.387468  0.58615    0.397755   0.292034   0.216963  0.932639  0.248254   0.137529  0.744861

(be careful in all these cases: do you want Z to be the exact same array, or a copy of it?)

2 Likes

Thanks! Yes, that’s what I need.

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.