Is it possible to have arrays with more then one dimension in Basic4Android? - basic4android

in the docu I found
ARRAY -> creates arrays with one dimension
DIM a() as string -> Array with strings in one dimension
MAP -> 1 Key, 1 Data field
do I miss something ?

For example:
Dim TwoDimensions (3,3) As Double
Dim ThreeDimensions(3,3,3) As Double
This you can find from section 10.3.2 in Beginners Guide, which is available to download from:
http://www.basic4ppc.com/android/documentation.html

Related

Create a fixed size 3 dimensional array of type string

I am trying to create a fixed size 3 dimensional array in Swift with datatype of string like this:
var 3dArray:[[[String]]] = [[[50]],[[50]],[[50]]]
But it is giving error:
Cannot convert value of type 'Int' to expected element type string
From your question title you are trying to create 3d array with datatype [[[String]]] and in your question detail you are assigning it an Int value which is not possible in swift. That's why you are getting an error. Because your 3dArray object is expecting a String values
Either you can assign String values this way:
var yourArr: [[[String]]] = [[["50"]],[["50"]],[["50"]]]
Or if you want to make Int array you can do it this way:
var yourArr: [[[Int]]] = [[[50]],[[50]],[[50]]]
Note:
As #Martin R suggested variable names cannot start with a digit.
There is no typical way of creating multidimensional arrays in Swift.
But, you can create an array which will contain only String arrays and then append 3 different String arrays. Please find the code below:
var array = [[String]]()
array.append([String]())
array.append([String]())
array.append([String]())
At the end your array will be looking this way:
[[], [], []]
Or you can just create same array this way:
var array:[[String]] = [["50"],["50"],["50"]]
But keep in mind, "50" are strings which are content of those arrays and not their size.
If you want to have a fixed-size array, then you have to use some third party solutions like this one.

Evaluate Equation from String

I have a String stored in a table with two logic gates which looks like 1*((CUBL>1)*((DIFL>1)*1)).
I have been able to replace the text with values from a different table i.e. 1*((1500>1)*((0>1)*1)). The issue i am having is being able to evaluate the equations between the brackets. The following is a list of steps that i believe are needed.
1*((CUBL>1)*((DIFL>1)*1)) -> 1*((1500>1)*((0>1)*1))
1*((1500>1)*((0>1)*1)) -> 1*(1*(0*1))
1*(1*(0*1)) -> 1*(1*0)
1*(1*0) -> 1*0 = 0

Convert Matlab struct array to cell array

Can a Matlab struct array be converted to a cell array without iterating through the array?
I want each struct in the struct array to become one cell in the cell array. The command struct2cell doesn't seem to do it as it breaks out each field in the struct into a separate cell.
This has been posted to:
Convert Matlab struct array to cell array
http://groups.google.com/forum/#!topic/comp.soft-sys.matlab/xIOTcs5HPeg
Try num2cell:
myStructCell = num2cell(myStruct);
For example:
>> myStruct(1).name = 'John';
>> myStruct(2).name = 'Paul';
>> myStruct
myStruct =
1x2 struct array with fields:
name
>> myStructCell = num2cell(myStruct)
myStructCell =
[1x1 struct] [1x1 struct]
>> myStructCell{1}
ans =
name: 'John'
>> myStructCell{2}
ans =
name: 'Paul'
>> myStructCell{2}.name
ans =
Paul
Actually, I don't think that what I'm trying to do is necessary. Let me explain, in case it saves someone else from going down the same path.
The motivation for the above is that I want to extract a certain subfield from all structures in the struct array and have it in the form of a comma separated list:
myStruc(1).fieldX.subfieldA, ...
myStruc(2).fieldX.subfieldA, ...
myStruc(3).fieldX.subfieldA
I knew that I could generate a comma separated list by indexing into all cells into a 1D cell array via myCellArray{:}.
However, I found that there was actually an entire help page entitled "Comma-Separated Lists" showing that structs behave in the same way. So the above comma separated list is equal to myStruc(:).fieldX.subfieldA.
In fact, converting the struct array into a cell array wouldn't have worked because you can't use dot-indexing to access the fields after curly-brace indexing of the cell array. For example, if there was a vectorized way to convert myStruct(i) into myCell(i), I was hoping to be able to generate
myCellArray{1}.fieldX.subfieldA, ...
myCellArray{2}.fieldX.subfieldA, ...
myCellArray{3}.fieldX.subfieldA
via the expression myCell{:}.fieldX.subfieldA. The dot-indexing after the curly braces is a syntax error.
Lesson learned: Use struct array indexing directly to enable access to the struct fields & subfields.
***** CAVEAT *****
I only tested the generation of comma separated lists using multiple levels of dot-indexing combined with a scalar numerical array index, e.g., myCellArray{2}.fieldX.subfieldA. It doesn't work when with a vector numerical index in place of the scalar value 2, i.e., Matlab cannot handle myCellArray{:}.fieldX.subfieldA or myCellArray{2:3}.fieldX.subfieldA.
Oh well. :(

Binding a 2D array of doubles to a parameter in MS Solver Foundation

How to bind a 2D array to a parameter in Solver Foundation? Have tried defining the array as double(,); as double()() and as a list of tuples(double, i, j).
I have also tried to implement the extension methods to SetBinding, suggested here; http://blogs.msdn.com/b/solverfoundation/archive/2010/06/28/simpler-data-binding-using-linq-and-extension-methods.aspx
Currently fails at third line to bottom; m_cov.SetBinding(CovMatrix), with error "This method is only valid when called on parameters with 0 indexes"
I'm using latest version and working in vb.net. Any help appreciated.
Thanks,
Yug
Public Sub ERC()
Dim m_i = New [Set](Domain.Any, "I")
Dim m_j = New [Set](Domain.Any, "J")
'Dim m_allocation As Decision
Dim CovMatrix As Double()() = {New Double() {0.1, 0.15, 0.4}, New Double() {0.3, 0.5, 0.8}, New Double() {0, 0.33, 0.05}}
Dim m_context As SolverContext = SolverContext.GetContext()
Dim m_model As Model = m_context.CreateModel()
m_model.Name = "ERC"
' Create a Parameter for Cov
Dim m_cov = New Parameter(Domain.Real, "Cov", m_i, m_j)
m_model.AddParameter(m_cov)
' Create a Decision for Allocation
Dim m_allocation As Decision = New Decision(Domain.RealRange(-1.0, 1.0), "Allocation", m_i)
m_model.AddDecision(m_allocation)
' Add Constraint for SumWts
m_model.AddConstraint("SumWts", (Model.Sum(Model.ForEach(m_i, Function(i_1) Model.Abs(Model.Sum(m_allocation(i_1)))))) = 1.0)
' Add Goal for Variance
m_model.AddGoal("Variance", GoalKind.Minimize, Model.Sum(Model.ForEach(m_i, Function(i_2) Model.ForEach(m_j, Function(j_3) Model.Power((Model.Abs(Model.Sum(Model.ForEach(m_j, Function(j_4) Model.Product(m_cov(i_2, j_4), m_allocation(j_4), m_allocation(i_2))))) - Model.Abs(Model.Sum(Model.ForEach(m_j, Function(j_6) Model.Product(m_cov(j_3, j_6), m_allocation(j_6), m_allocation(j_3)))))), 2.0)))))
m_cov.SetBinding(CovMatrix)
m_context.Solve()
Debug.Print(m_allocation.GetValuesByIndex().ToString)
End Sub
The helper class provided by Nathan Brixius makes SetBinding way easier. His helper class is in C#, so I went ahead and converted the particular helper function you will need into VB (see below).
The exception is telling you that the SetBinding function needs to know the indices for the data passed in. MSF is built to handle generic domains, meaning it does not abide by normal array indices. You have to explicitly point out the index information.
The problem with your code is that you are trying to pass in raw arrays without any extra index data. To remedy this on a normal 1D array, you would add indices using KeyValuePair(Of Integer, Double). In this case for a matrix you need a list of Tuple (index1, index2, Double). Essentially, you need to flatten the 3x3 matrix into 9 triples, specifying each value according to a pair of indices.
Here is the VB function to convert your matrix into a list as such:
Private Function ToIEnumerable(Of T)(matrix As IEnumerable(Of IEnumerable(Of T))) As IEnumerable(Of Tuple(Of Integer, Integer, T))
Dim m = matrix.[Select](Function(row, i) row.[Select](Function(cell, j) New Tuple(Of Integer, Integer, T)(i, j, cell)))
Dim cells = From cell In m.SelectMany(Function(c) c)
Return cells
End Function
Include this function in your class, and then change the SetBinding line of code like so:
m_cov.SetBinding(ToIEnumerable(CovMatrix), "Item3", "Item1", "Item2")
Notice the ordering of the Tuple items! By MSF convention, the value field comes before the indices. The same ordering is returned in the Solution output (important to note when you are looking to iterate over the Decisions on the result set).
If you convert the rest of Nathan's helper class, he makes it even easier by overloading the SetBinding function itself to abstract away the ToIEnumerable(data) call as well as the key/value identifier ordering. Then you are able to simply call model.SetBinding(rawMatrix).
Pretty slick, eh? ;)

Iterating through struct fieldnames in MATLAB

My question is easily summarized as: "Why does the following not work?"
teststruct = struct('a',3,'b',5,'c',9)
fields = fieldnames(teststruct)
for i=1:numel(fields)
fields(i)
teststruct.(fields(i))
end
output:
ans = 'a'
??? Argument to dynamic structure reference must evaluate to a valid field name.
Especially since teststruct.('a') does work. And fields(i) prints out ans = 'a'.
I can't get my head around it.
You have to use curly braces ({}) to access fields, since the fieldnames function returns a cell array of strings:
for i = 1:numel(fields)
teststruct.(fields{i})
end
Using parentheses to access data in your cell array will just return another cell array, which is displayed differently from a character array:
>> fields(1) % Get the first cell of the cell array
ans =
'a' % This is how the 1-element cell array is displayed
>> fields{1} % Get the contents of the first cell of the cell array
ans =
a % This is how the single character is displayed
Since fields or fns are cell arrays, you have to index with curly brackets {} in order to access the contents of the cell, i.e. the string.
Note that instead of looping over a number, you can also loop over fields directly, making use of a neat Matlab features that lets you loop through any array. The iteration variable takes on the value of each column of the array.
teststruct = struct('a',3,'b',5,'c',9)
fields = fieldnames(teststruct)
for fn=fields'
fn
%# since fn is a 1-by-1 cell array, you still need to index into it, unfortunately
teststruct.(fn{1})
end
Your fns is a cellstr array. You need to index in to it with {} instead of () to get the single string out as char.
fns{i}
teststruct.(fns{i})
Indexing in to it with () returns a 1-long cellstr array, which isn't the same format as the char array that the ".(name)" dynamic field reference wants. The formatting, especially in the display output, can be confusing. To see the difference, try this.
name_as_char = 'a'
name_as_cellstr = {'a'}
You can use the for each toolbox from http://www.mathworks.com/matlabcentral/fileexchange/48729-for-each.
>> signal
signal =
sin: {{1x1x25 cell} {1x1x25 cell}}
cos: {{1x1x25 cell} {1x1x25 cell}}
>> each(fieldnames(signal))
ans =
CellIterator with properties:
NumberOfIterations: 2.0000e+000
Usage:
for bridge = each(fieldnames(signal))
signal.(bridge) = rand(10);
end
I like it very much. Credit of course go to Jeremy Hughes who developed the toolbox.