0

I have the following problem: I have two arrays called Variable_1 and Variable_2 (same size 8x3, only different values). I need to make calculations with the values inside the array and store the result in a new array. The calculation is the same for both arrays.

Right now, I solved it with 2 for-loops.

for i = 1:size(Variable_1,1)
Calculation_1 = 5 * Variable_1(i,1);
Result_1(i,:) = Calculation_1;
end

for i = 1:size(Variable_2,1)
Calculation_2 = 5 * Variable_2(i,1);
Result_2(i,:) = Calculation_2;
end

I want to get rid of two separate for-loops and do it with one loop or a loop in a loop. The name "Variable_x" needs to be kinda dynamical. It is different for every run in the outer for-loop. First, it is Variable_1 and matlab has to look in the array "Variable_1" at position i. Later, it is Variable_2 and it has to look in another array, here called "Variable_2".

I know that I shouldn't use dynamic variables and however I didn't found a solution anyway. Maybe it works with Cellarrays but I dont't know exactly how I use them in this specific case.

It is kinda hard to explain, so feel free to ask questions if you have one. I am really looking forward for any suggestions.

  • 1
    Something [like this](https://stackoverflow.com/q/466972/52738)? If your variables are the same size and store the same type of data, you can just use a multi-dimensional array, then avoid for loops by [performing operations across dimensions](https://stackoverflow.com/q/2307249/52738). – gnovice May 02 '19 at 18:19
  • 1
    Just replace `Variable_1` with `Variable{1}` and `Variable_2` with `Variable{2}` everywhere in your code. From there, you should be able to figure out how to write that loop. – Cris Luengo May 02 '19 at 18:39
  • It worked for my with the Variable{1} version. Thanks for that! – Scarybyte May 02 '19 at 19:45

0 Answers0