Dear GAUSS users,
I have data for 85 series and would like to write a code to define new series by dividing them to each other.That is, I want Xi/Xj, where i,j=1,2,..., 85 and also I want to name the new series Xij. Could anyone help me on this issue. I have problem on placing the new series in the right column.
many thanks in advance,
Firouz
4 Answers
0
I am not completely certain that I understand what you are trying to do. However, here are a couple of options that I think will do something like what you are looking for.
new;
//Create a 3 column matrix
x = { 3 20 11 5,
17 11 23 3,
19 14 1 9,
7 14 17 14,
4 13 4 11 };
x_out = divideAllSeries(x);
print x_out;
proc (1) = divideAllSeries(x);
local numerator, denominator, orders, out;
//Create denominator to contain cols(x)
//copies of x
orders = cols(x) | rows(x) | cols(x);
denominator = areshape(x, orders);
//Create 3-D array to hold numerator
orders = cols(x) | rows(x) | 1;
numerator = areshape(x', orders);
//Perform division in one step
out = numerator ./ denominator;
retp(out);
endp;
which will return:
Plane [1,.,.] 1.0000 0.1500 0.2727 0.6000 1.0000 1.5455 0.7391 5.6667 1.0000 1.3571 19.0000 2.1111 1.0000 0.5000 0.4118 0.5000 1.0000 0.3077 1.0000 0.3636 Plane [2,.,.] 6.6667 1.0000 1.8182 4.0000 0.6471 1.0000 0.4783 3.6667 0.7368 1.0000 14.0000 1.5556 2.0000 1.0000 0.8235 1.0000 3.2500 1.0000 3.2500 1.1818 Plane [3,.,.] 3.6667 0.5500 1.0000 2.2000 1.3529 2.0909 1.0000 7.6667 0.0526 0.0714 1.0000 0.1111 2.4286 1.2143 1.0000 1.2143 1.0000 0.3077 1.0000 0.3636 Plane [4,.,.] 1.6667 0.2500 0.4545 1.0000 0.1765 0.2727 0.1304 1.0000 0.4737 0.6429 9.0000 1.0000 2.0000 1.0000 0.8235 1.0000 2.7500 0.8462 2.7500 1.0000
or this:
new;
//Create a 3 column matrix
x = { 3 20 11 5,
17 11 23 3,
19 14 1 9,
7 14 17 14,
4 13 4 11 };
x_out = divideAll(x);
print x_out;
proc (1) = divideAll(x);
local i, n, x_ij, idx, numerator, denominator, denominator_idx;
//Allocate array to hold final data
n = cols(x);
x_ij = arrayalloc(n|rows(x)|cols(x)-1, 0);
//Make sequence of 1 to cols(x) for later indexing
idx = seqa(1, 1, n);
for i(1, n, 1);
//numerator is i'th column
numerator = x[.,i];
//Remove i from the list of indices
denominator_idx = delif(idx, i .== idx);
//denominator is all columns EXCEPT the i'th
denominator = x[.,denominator_idx];
//Divide i'th column by all others
x_ij[i,.,.] = numerator ./ denominator;
endfor;
retp(x_ij);
endp;
which will return:
Plane [1,.,.] 0.1500 0.2727 0.6000 1.5455 0.7391 5.6667 1.3571 19.0000 2.1111 0.5000 0.4118 0.5000 0.3077 1.0000 0.3636 Plane [2,.,.] 6.6667 1.8182 4.0000 0.6471 0.4783 3.6667 0.7368 14.0000 1.5556 2.0000 0.8235 1.0000 3.2500 3.2500 1.1818 Plane [3,.,.] 3.6667 0.5500 2.2000 1.3529 2.0909 7.6667 0.0526 0.0714 0.1111 2.4286 1.2143 1.2143 1.0000 0.3077 0.3636 Plane [4,.,.] 1.6667 0.2500 0.4545 0.1765 0.2727 0.1304 0.4737 0.6429 9.0000 2.0000 1.0000 0.8235 2.7500 0.8462 2.7500
Do either of those work for you?
0
Many thanks for your help. I appreciate your help very much.
Both of the above codes create correct results but the first one is easier for me to understand. Could you please let me know how I can assign each column a name; such as X $+ "no of numerator" $+ "no of denominator" to know where this new series comes from?
Also I can save the results as a multi-dimensional matrix in GAUSS but is there a way to save them in a format that can be opened in EXCEL?
thanks again,
Firouz
0
Keeping track of the data
One nice thing about the data from the first procedure above, is that you can use indexing to keep track of where the data came from. If you want to find the data for where the i'th column was divided by the j'th column, you could simply do this:
x_ij = x_out[i, ., j];
For example, if after running the procedure exactly as it is in the first response, you wanted to access the column that results from the 2nd column is divided by the 4th column, you would use this:
x_12 = x_out[2, ., 4];
You could go through a loop and create, x_12, x_13, x_14, etc., but I would strongly recommend against this in any situation where you will have all of these observations loaded. If you will only be working with one set at a time, it might be different.
Saving this data in an Excel file
You have many options on how you store this data in Excel. You could transform the data into a 2 dimensional matrix and then write that to Excel. While that would work, it would make it more complicated than it needs to be when you were looking at the data in Excel or when you were trying to load it again. For these reasons, I would recommend, writing each of the submatrices in the 3-D array to a different sheet in Excel. For example:
xlsWrite3D(x_out, "myseries.xlsx");
proc (0) = xlsWrite3D(a, filename);
local orders, i, j, mat_tmp, num_mats, ret;
//Find out how many submatrices we have
orders = getorders(a);
num_mats = orders[1];
for i(1, num_mats, 1);
//Set 'mat_tmp' equal to the ith submatrix
mat_tmp = arraytomat(a[i, ., .]);
//Write the ith submatrix to the ith sheet in the Excel file
ret = xlsWriteM(mat_tmp, filename, "A1", i, "");
endfor;
endp;
0
It worked as a charm. Many many thanks for your help. I really appreciate it.
Firouz
Your Answer
4 Answers
I am not completely certain that I understand what you are trying to do. However, here are a couple of options that I think will do something like what you are looking for.
new;
//Create a 3 column matrix
x = { 3 20 11 5,
17 11 23 3,
19 14 1 9,
7 14 17 14,
4 13 4 11 };
x_out = divideAllSeries(x);
print x_out;
proc (1) = divideAllSeries(x);
local numerator, denominator, orders, out;
//Create denominator to contain cols(x)
//copies of x
orders = cols(x) | rows(x) | cols(x);
denominator = areshape(x, orders);
//Create 3-D array to hold numerator
orders = cols(x) | rows(x) | 1;
numerator = areshape(x', orders);
//Perform division in one step
out = numerator ./ denominator;
retp(out);
endp;
which will return:
Plane [1,.,.] 1.0000 0.1500 0.2727 0.6000 1.0000 1.5455 0.7391 5.6667 1.0000 1.3571 19.0000 2.1111 1.0000 0.5000 0.4118 0.5000 1.0000 0.3077 1.0000 0.3636 Plane [2,.,.] 6.6667 1.0000 1.8182 4.0000 0.6471 1.0000 0.4783 3.6667 0.7368 1.0000 14.0000 1.5556 2.0000 1.0000 0.8235 1.0000 3.2500 1.0000 3.2500 1.1818 Plane [3,.,.] 3.6667 0.5500 1.0000 2.2000 1.3529 2.0909 1.0000 7.6667 0.0526 0.0714 1.0000 0.1111 2.4286 1.2143 1.0000 1.2143 1.0000 0.3077 1.0000 0.3636 Plane [4,.,.] 1.6667 0.2500 0.4545 1.0000 0.1765 0.2727 0.1304 1.0000 0.4737 0.6429 9.0000 1.0000 2.0000 1.0000 0.8235 1.0000 2.7500 0.8462 2.7500 1.0000
or this:
new;
//Create a 3 column matrix
x = { 3 20 11 5,
17 11 23 3,
19 14 1 9,
7 14 17 14,
4 13 4 11 };
x_out = divideAll(x);
print x_out;
proc (1) = divideAll(x);
local i, n, x_ij, idx, numerator, denominator, denominator_idx;
//Allocate array to hold final data
n = cols(x);
x_ij = arrayalloc(n|rows(x)|cols(x)-1, 0);
//Make sequence of 1 to cols(x) for later indexing
idx = seqa(1, 1, n);
for i(1, n, 1);
//numerator is i'th column
numerator = x[.,i];
//Remove i from the list of indices
denominator_idx = delif(idx, i .== idx);
//denominator is all columns EXCEPT the i'th
denominator = x[.,denominator_idx];
//Divide i'th column by all others
x_ij[i,.,.] = numerator ./ denominator;
endfor;
retp(x_ij);
endp;
which will return:
Plane [1,.,.] 0.1500 0.2727 0.6000 1.5455 0.7391 5.6667 1.3571 19.0000 2.1111 0.5000 0.4118 0.5000 0.3077 1.0000 0.3636 Plane [2,.,.] 6.6667 1.8182 4.0000 0.6471 0.4783 3.6667 0.7368 14.0000 1.5556 2.0000 0.8235 1.0000 3.2500 3.2500 1.1818 Plane [3,.,.] 3.6667 0.5500 2.2000 1.3529 2.0909 7.6667 0.0526 0.0714 0.1111 2.4286 1.2143 1.2143 1.0000 0.3077 0.3636 Plane [4,.,.] 1.6667 0.2500 0.4545 0.1765 0.2727 0.1304 0.4737 0.6429 9.0000 2.0000 1.0000 0.8235 2.7500 0.8462 2.7500
Do either of those work for you?
Many thanks for your help. I appreciate your help very much.
Both of the above codes create correct results but the first one is easier for me to understand. Could you please let me know how I can assign each column a name; such as X $+ "no of numerator" $+ "no of denominator" to know where this new series comes from?
Also I can save the results as a multi-dimensional matrix in GAUSS but is there a way to save them in a format that can be opened in EXCEL?
thanks again,
Firouz
Keeping track of the data
One nice thing about the data from the first procedure above, is that you can use indexing to keep track of where the data came from. If you want to find the data for where the i'th column was divided by the j'th column, you could simply do this:
x_ij = x_out[i, ., j];
For example, if after running the procedure exactly as it is in the first response, you wanted to access the column that results from the 2nd column is divided by the 4th column, you would use this:
x_12 = x_out[2, ., 4];
You could go through a loop and create, x_12, x_13, x_14, etc., but I would strongly recommend against this in any situation where you will have all of these observations loaded. If you will only be working with one set at a time, it might be different.
Saving this data in an Excel file
You have many options on how you store this data in Excel. You could transform the data into a 2 dimensional matrix and then write that to Excel. While that would work, it would make it more complicated than it needs to be when you were looking at the data in Excel or when you were trying to load it again. For these reasons, I would recommend, writing each of the submatrices in the 3-D array to a different sheet in Excel. For example:
xlsWrite3D(x_out, "myseries.xlsx");
proc (0) = xlsWrite3D(a, filename);
local orders, i, j, mat_tmp, num_mats, ret;
//Find out how many submatrices we have
orders = getorders(a);
num_mats = orders[1];
for i(1, num_mats, 1);
//Set 'mat_tmp' equal to the ith submatrix
mat_tmp = arraytomat(a[i, ., .]);
//Write the ith submatrix to the ith sheet in the Excel file
ret = xlsWriteM(mat_tmp, filename, "A1", i, "");
endfor;
endp;
It worked as a charm. Many many thanks for your help. I really appreciate it.
Firouz