Saving individual arrays from a loop in VBA - arrays

So I have an array x(i,j) which is inside a loop k = 1 to n, where n is set by the user. It updates every time I go to the next k. I was wondering if it's possible to save each of the arrays created in the loop to the memory so I can recall them at another time?
So for example, I was thinking I could do something along the lines of:
For k = 1 to n
...
SavedArray(k) = x(i,j)
...
next k
So I could recall, say for example, SavedArray(58) which would have been the 58th iteration of the loop.
Is this feasible?
Thanks for your time.

It depends what is X, and what you wish to save.
If X(i,j) is just Integer so just make a new array and save it : MyArr(k) = X(i,j)
If X(i,j) is array (so X is 2D of 1D array ~ 3D) -you can use Array of Arrays to save it.
To simplify the solution you can create a new type, let say:
Public Type typeArray
X(1 To 10) As Integer
Y(1 To 10) As Integer
End Type
And Declare:
Public SavedArray(1 To 10) As typeArray
Now you can use it as you wish.

Related

Summation in a loop in fortran

lets say I have the following array: [1,2,3,4,5,6,7,8,9,10]
I need to define a new array in a way that:
for i=1, the first element of this new array will have the sum of only the first element (so returns the first element of the array)
for i=2, the second element of this array has the sum of the first two elements. etc...
then I need to fill each sum in this array.
Any idea how to do it? I'm new to Fortran and cant figure out how to do it, maybe there is a function to do it.
The following does what you want. Instead of using implied-do loops, you could use explicit loops.
program addup
implicit none
integer, allocatable :: a(:), b(:)
integer i, n
n = 10
a = [(i,i=1,n)]
b = [(sum(a(1:i)),i=1,n)]
print '(10(I0,1x))', a
print '(10(I0,1x))', b
end program addup

Looping through a set of sequences satisfying a certain property, without storing them

Below is a MATLAB code (recursion) which inputs a vector (l_1,l_2,...,l_r) of non negative integers and an integer m prints all sequences (m_1,m_2,...,m_r) satisfying:
0 <= m_i <= l_i for all 1 <= i <= r and m_1 + m_2 + ... + m_r = m
The r is captured in the function definition by calling the size of the (l_i) array below:
function arr=sumseq(m,lims)
arr=[];
r=size(lims,2);
if r==0 || m<0
arr=[];
elseif r==1 && lims(1)>=m
arr=[m]; %#ok<NBRAK>
else
for i=0:lims(1)
if(lims(1)<0)
arr=[];
else
v=sumseq(m-i,lims(2:end));
arr=[arr;[i*ones(size(v,1),1) v]];
end
end
end
end
Here what I have done is, stored a whole array of them and made it my output. Instead I want to only print them one by one and not store them in an array. This seems simple enough as there is not much choice in which line(s) I need to change (I believe it is the contents of the else block inside the for loop), but I get into a fix every time I try to achieve it.
(Also, MATLAB warned me that if I kept re-initializing the array with a larger array like in the statement:
arr=[arr;[i*ones(size(v,1),1) v]];
it reallocates a fresh array for all the contents of arr and spends a 'lot' of time doing so.)
In short: recursion or not, I want to save the trouble of storing it, and need an algorithm which is as efficient as or more efficient than what I have here.

Modelica flexible array size within record - failed to expand

I'm trying to read in multiple files (csv, 2 columns) with belonging names (String) and interpolation methods smoothness (String). By using a record I get a nice GUI in Dymola:
To explain my problem, here is a simplified model:
model Test_Strings
parameter String x[:];
parameter Integer k = size(x,1);
Integer i;
initial algorithm
i := 0;
Modelica.Utilities.Streams.print(String(i));
Modelica.Utilities.Streams.print(String(k));
equation
algorithm
when sample(1,1) then
i :=pre(i) + 1;
Modelica.Utilities.Streams.print(String(i));
for j in 1:k loop
Modelica.Utilities.Streams.print(x[j]);
end for;
end when;
end Test_Strings;
The GUI for this looks as follows:
The code to run it:
model Test_Strings_System
Test_Strings test_Strings(x={"a","b","c","d"});
end Test_Strings_System;
This will give the following result in the console:
Now, if I try to use a record:
record MyRecord2
parameter String name = "NoName";
end MyRecord2;
And adapt the model (only the first parameter line MyRecord2 x[:] and within the for loop x[j].name changed):
model Test_Strings2
parameter MyRecord2 x[:];
parameter Integer k = size(x,1);
Integer i;
initial algorithm
i := 0;
Modelica.Utilities.Streams.print(String(i));
Modelica.Utilities.Streams.print(String(k));
equation
algorithm
when sample(1,1) then
i :=pre(i) + 1;
Modelica.Utilities.Streams.print(String(i));
for j in 1:k loop // k must be fixed number if equation
Modelica.Utilities.Streams.print(x[j].name); // j must be fixed number if algorithm
end for;
end when;
end Test_Strings2;
Then I get a translation error: Internal error: failed to expand string.
If I fix k or j within the for loop to a given number (let's say 3) then it works, but depending if it's within an algorithm or equation section (see comments within code).
I had similar problems with flexible array sizes and still don't understand how to solve it.
Do I have to use functions?
How can I use flexible array sizes which are defined depending on external data, selected as parameters prior to simulation (e.g. the length of a table)?
Or is the problem in this case somewhere else?
Thanks.
You can change the model to have an array of records as visible parameters, but internally use an array of strings (tested with Dymola 2017 and later):
model Test_Strings2
parameter MyRecord2 x[:];
parameter Integer k = size(x,1);
Integer i;
protected
parameter String s[:]=x.name; // Hack
initial algorithm
i := 0;
Modelica.Utilities.Streams.print(String(i));
Modelica.Utilities.Streams.print(String(k));
equation
algorithm
when sample(1,1) then
i :=pre(i) + 1;
Modelica.Utilities.Streams.print(String(i));
for j in 1:k loop
Modelica.Utilities.Streams.print(s[j]);
end for;
end when;
end Test_Strings2;

Subtracting from each element in array in python

I've got a sort of array in python, and i'm looking to subtract one from each int in all of it. for example:
arr = [[2,3,4],
[5,6,7],
[8,9,10]]
#this should become this:
arr = [[1,2,3],
[4,5,6],
[7,8,9]]
there's a few ways i've tried to do this
for i in arr:
for j in i:
j-=1 #doesn't work!
I'm aware it would be easier to do this with numpy, but this is for a large project that i'm working on, so implementing numpy would take hours, if not more. Thanks!
So the way that you are doing it is just reassigning the variable j within your loop to one less its original value. However, what you want to do is reassign the value of the array AT the index j to one less its original value. To do this without using numpy, simply loop through all the indexes of the array, and then replace the value:
for i in range(len(arr)):
for j in range(len(arr[i])):
arr[i][j] -= 1
If you're unsure of why this is, look into how variable assignment works in Python.
You could use a nested list comprehension:
arr = [[y - 1 for y in x] for x in arr]
Your solution didn't work, because jis a copy of the value from your array, rather than a pointer to the array item itself.
Below is sample code that works. Essentially, iterate through each location in the array, and modify the original array at that location.
arr = [[2,3,4],
[5,6,7],
[8,9,10]]
for x_idx, x in enumerate(arr):
for y_idx, y in enumerate(x):
arr[x_idx][y_idx] -= 1
print(arr)

Unable to do vector division (every element) and append to a new array

I need to append the division between two arrays into a new array. But the same value being iteratively stored into the new array.
clear; close all;
%Plot G (small - signal gain) vs v (frequency)
%Assume frequency probe region to be thrice the line-width
f=input('Line Width Frequency(in Hz) :');
v=(-3*f):10e10:(3*f);
l=length(v);
g=zeros(1,l);
k=zeros(1,l);
h=ones(1,l);
% Substituing the constants we realize g(v) is a function
% line shaping function
for i=1:l
k(1:i)=((v(1:i)-2.82*10e-4)+((627.95*10e9)^2));
end
g=rdivide(h,k);
You are assigning multiple elements of k at once. Try this:
for i=1:l
k(i)=((v(i)-2.82*10e-4)+((627.95*10e9)^2));
end
or even more simply, this is a great example of a vectorized operation:
k = (v -2.82*10e-4)+((627.95*10e9)^2)); %% works the same as above
k(1:i) is a slice of the array k, containing the elements 1, 2, .. i. So in the last loop iteration, you assign the entire array k(1:l), wiping out the calculations from previous iterations.

Resources