How to save result from loop and save them in a new array? - idl-programming-language

I am trying to save the results of loop in a new array, then plot them.
But now I can only save the last value comes from the loop. How can I save all the results from the loop?
for j=1,200 do begin
h = where(o eq j,ct3)
if (ct3 ne 0) then begin
mag = a1[h].imag
bcg = min(mag)
deltay = pqq[plu2[j]]
bcg1 = float(bcg)
u = where(bcg1*deltay ne 0)
bcg2 = bcg1[u]
deltay1 = deltay[u]
print,deltay1,bcg2
plot,bcg2,deltay1,psym=5
endif
endfor

To store a variable number of values each time through your loop, I would use a list and then the toArray method when you want the final array to plot.
For example, at the beginning of your code create a list to store the results in:
deltay_list = list()
Then in your loop, add elements to your list:
deltay_list->add, deltay1, /extract
The EXTRACT keyword indicates that you should add the individual elements of deltay1, not add deltay as a single element of the list. When you want to plot, then do:
deltay_array = deltay_list->toArray()
obj_destroy, deltay_list
plot, deltay_array

Related

How to use the "6" char code in overloading insertion function on Scilab?

In the Help Documentation of Scilab 6.0.2, I can read the following instruction on the Overloading entry, regarding the last operation code "iext" showed in this entry's table:
"The 6 char code may be used for some complex insertion algorithm like x.b(2) = 33 where b field is not defined in the structure x. The insertion is automatically decomposed into temp = x.b; temp(2) = 33; x.b = temp. The 6 char code is used for the first step of this algorithm. The 6 overloading function is very similar to the e's one."
But I can't find a complete example on how to use this "char 6 code" to overload a function. I'm trying to use it, without success. Does anyone have an example on how to do this?
The code bellow creates a normal "mlist" as a example. Which needs overloading functions
A = rand(5,3)
names = ["colA" "colB" "colC"]
units = ["ft" "in" "lb"]
M = mlist(["Mlog" "names" "units" names],names,units,A(:,1),A(:,2),A(:,3))
Following are the overload functions:
//define display
function %Mlog_p(M)
n = size(M.names,"*")
formatStr = strcat(repmat("%10s ",1,n)) + "\n"
formatNum = strcat(repmat("%0.10f ",1,n)) + "\n"
mprintf(formatStr,M.names)
mprintf(formatStr,M.units)
disp([M(M.names(1)),M(M.names(2)),M(M.names(3))])
end
//define extraction operation
function [Mat]=%Mlog_e(varargin)
M = varargin($)
cols = [1:size(M.names,"*")] // This will also work
cols = cols(varargin($-1)) // when varargin($-1) = 1:1:$
Mat = []
if length(varargin)==3 then
for i = M.names(cols)
Mat = [Mat M(i)(varargin(1))]
end
else
for i=1:size(M.names(cols),"*")
Mat(i).name = M.names(cols(i))
Mat(i).unit = M.units(cols(i))
Mat(i).data = M(:,cols(i))
end
end
endfunction
//define insertion operations (a regular matrix into a Mlog matrix)
function ML=%s_i_Mlog(i,j,V,M)
names = M.names
units = M.units
A = M(:,:) // uses function above
A(i,j) = V
ML = mlist(["Mlog" "names" "units" names],names,units,A(:,1),A(:,2),A(:,3))
endfunction
//insertion operation with structures (the subject of the question)
function temp = %Mlog_6(j,M)
temp = M(j) // uses function %Mlog_e
endfunction
function M = %st_i_Mlog(j,st,M)
A = M(:,:) // uses function %Mlog_e
M.names(j) = st.name // uses function above
M.units(j) = st.unit // uses function above
A(:,j) = st.data // uses function above
names = M.names
units = M.units
M = mlist(["Mlog" "names" "units" names],names,units,A(:,1),A(:,2),A(:,3))
endfunction
The first overload (displays mlist) will show the matrix in the form of the following table:
--> M
M =
colA colB colC
ft in lb
0.4720517 0.6719395 0.5628382
0.0623731 0.1360619 0.5531093
0.0854401 0.2119744 0.0768984
0.0134564 0.4015942 0.5360758
0.3543002 0.4036219 0.0900212
The next overloads (extraction and insertion) Will allow the table to be access as a simple matrix M(i,j).
The extraction function Will also allow M to be access by column, which returns a structure, for instance:
--> M(2)
ans =
name: "colB"
unit: "in"
data: [5x1 constant]
The last two functions are the overloads mentioned in the question. They allow the column metadata to be changed in a structure form.
--> M(2).name = "length"
M =
colA length colC
ft in lb
0.4720517 0.6719395 0.5628382
0.0623731 0.1360619 0.5531093
0.0854401 0.2119744 0.0768984
0.0134564 0.4015942 0.5360758
0.3543002 0.4036219 0.0900212

Store plots in an array

I am trying to plot histograms of different columns of a dataframe in subplots.
plt_count = 1
for i = names(abalone)[2:end]
p[plt_count]=histogram(abalone[:,i])
plt_count += 1
end
plot(p, layout=(3,3), legend=false)
This is what I tried. But I can't come up with the right definition for the array p. How do I define p?
Improvements to the code will also be helpful.
If you don't care about the type stability, you can make Any type array.
ps = Array{Any}(nothing, 3)
ps[1] = plot([2,3,4])
ps[2] = plot([1,5])
ps[3] = plot([10,5,1,0])
#show typeof(ps)
plot(ps..., layout=(3,1))
If you want to create an array of Plot type specifically, one approach is to initialize an array with a dummy plot, then replace later.
ps = repeat([plot(1)], 3)
ps[1] = plot([2,3,4])
ps[2] = plot([1,5])
ps[3] = plot([10,5,1,0])
#show typeof(ps)
plot(ps..., layout=(3,1))

scilab submatrix incorrectly defined

I am stuck at creating a matrix of a matrix (vector in this case)
What I have so far
index = zeros(size(A)) // This is some matrix but isn't important to the question
indexIndex = 1;
for rows=1:length(R)
for columns=1:length(K)
if(A(rows,columns)==x)
V=[rows columns]; // I create a vector holding the row + column
index(indexIndex) = V(1,2) // I want to store all these vectors
indexIndex = indexIndex + 1
end
end
end
I have tried various ways of getting the information out of V (such as V(1:2)) but nothing seems to work correctly.
In other words, I'm trying to get an array of points.
Thanks in advance
I do not understand your question exactly. What is the size of A? What is x, K and R? But under some assumptions,
Using list
You could use a list
// Create some matrix A
A = zeros(8,8)
//initialize the list
index = list();
// Get the dimensions of A
rows = size(A,1);
cols = size(A,2);
x = 0;
for row=1:rows
for col=1:cols
if(A(row,col)==x)
// Create a vector holding row and col
V=[row col];
// Append it to list using $ (last index) + 1
index($+1) = V
end
end
end
Single indexed matrices
Another approach would be to make use of the fact an multi-dimensional matrix can also be indexed by a single value.
For instance create a random matrix named a:
-->a = rand(3,3)
a =
0.6212882 0.5211472 0.0881335
0.3454984 0.2870401 0.4498763
0.7064868 0.6502795 0.7227253
Access the first value:
-->a(1)
ans =
0.6212882
-->a(1,1)
ans =
0.6212882
Access the second value:
-->a(2)
ans =
0.3454984
-->a(2,1)
ans =
0.3454984
So that proves how the single indexing works. Now to apply it to your problem and knocking out a for-loop.
// Create some matrix A
A = zeros(8,8)
//initialize the array of indices
index = [];
// Get the dimensions of A
rows = size(A,1);
cols = size(A,2);
x = 0;
for i=1:length(A)
if(A(i)==x)
// Append it to list using $ (last index) + 1
index($+1) = i;
end
end
Without for-loop
If you just need the values that adhere to a certain condition you could also do something like this
values = A(A==x);
Be carefull when comparing doubles, these are not always (un)equal when you expect.

Is there a way to access an entire row with the GetRows() method?

I have used the GetRows() method to put a recordset into a 2D array. I can access individual array items like this:
x = rows(colNumber, rowNumber)
Now I want to take a whole row / dimension from this array and pass it into another function.
Is there a method for doing this? I haven't been able to find one. Looking for something like this:
entireSingleRow = rows(*, rowNumber)
There isn't such construct in the language, but the following "helper" will do"
REM 'Returns 1-D array of all "columns" in a "row" with index "rowNumber"'
REM 'from the 2-D "from2DArray" array (column, row)'
Function GetCols(ByRef from2DArray, ByVal rowNumber)
Dim cols : cols = UBound(from2DArray, 1)
Dim i
Dim result()
For i = 0 To cols
Redim Preserve result(i)
result(i) = from2DArray(i, rowNumber)
Next
GetCols = result
End Function
entireSingleRow = GetCols(rows, rowNumber)

processing 2 dimension array

I am working in classic ASP; using getRows to get multidimension array of rows and column.
while iterating a row; I want to pass that single row into another function to build the column layout.
with C# I can do this:
obj[][] multiDimArray = FunctionCall_To_InitializeArray_4X16();
for (int rowId = 0 ; rowId < 4 ; rowId++)
{
FunctionCall_to_ProcessSingleRow(multiDimArray[rowId][]);
//this function only accept single dimension array
}
How can I do this is asp classic/vbscript:
1. I have a function that accept single dimension array as parameter.
2. Call that function and pass 1 part of 2 dimension array.
Thank you
I think you will need to populate a new array or dictionary object with the single dimension you want to process.
here a piece from working code, should get you going..
aResults = oRst.Getrows
oRst.Close
Set oRst = Nothing
Call SubCloseDatabaseOracle
iRows = UBound(aResults, 2)
iCols = UBound(aResults, 1)
row = 1 'first row
line = ""
separator = ""
FOR col = 0 TO iCols
line = line & separator & cStr(aResults(col,row))
separator = ";"
NEXT
aSingleDimensionArray = split(line,";")

Resources