How to make a dictionary of arrays? - arrays

I can't seem to figure out how containers.Map works. It's doing okay with characters and numbers, but flips out when I try to feed it arrays. How do I make something like this?
function test
global a
a = containers.Map();
a(pi) = 3:14;
a(5) = 4:2:10;
end

The problem is that you're using the default constructor for the containers.Map class. From the help:
myMap = containers.Map() creates an empty object myMap that is an
instance of the MATLAB containers.Map class. The properties of myMap
are Count (set to 0), KeyType (set to 'char'), and ValueType (set to
'any').
In other words, you can only use strings as keys in this form. If you want to use arbitrary double precision values as keys, you need to specify the 'KeyType' and 'ValueType' in the constructor:
myMap = containers.Map('KeyType', kType, 'ValueType', vType) constructs
a Map object with no data that uses a key type of kType, and a value
type of vType. Valid values for kType are the strings: 'char',
'double', 'single', 'int32', 'uint32', 'int64', 'uint64'. Valid values
for vType are the strings: 'char', 'double', 'single', 'int8', 'uint8',
'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'logical', or
'any'. The order of the key type and value type arguments is not
important, but both must be provided.
In your example:
a = containers.Map('KeyType','double','ValueType','any');
a(pi) = 3:14;
a(5) = 4:2:10;
Note, however, that a 'ValueType' of 'double' will not work for non-scalar values. Alternatively, you can specify your keys and values directly with cell arrays via the constructor and let it do the work of figuring out what types to use:
a = containers.Map({pi,5},{3:14 4:2:10});

There is no issue with putting arrays, cells, even other maps inside a map. The issue is your key field. As far as I know, Maps use strings as keys. So your current code won't work, but this would
function test
global a
a = containers.Map();
a('pi') = 3:14;
a('5') = 4:2:10;
end

Related

Type 'Array[]' is not assignable to type '[Array]'

This error keeps bugging me while I'm trying to get a grab on typescript. Can anyone explain what is the difference between the two 'array'? Is any of this not an array? If not, what is it then?
[ts] Type 'AppointmentItemData[]' is not assignable to type '[AppointmentItemData]'.
Property '0' is missing in type 'AppointmentItemData[]'.
Your first type (AppointmentItemData[]) is an array of AppointmentItemData elements, while your second type is a tuple, that may only ever contain a single AppointmentItemData. Quoting from the docs:
Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same. For example, you may want to represent a value as a pair of a string and a number:
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
Typescript allows you to encode that "this variable should only every have a 3 element array in it, where the first element is a number, the second is a string, and the third is a Promise that will eventually resolve to something shaped like a Lizard", whereas if it didn't the only way to encode [number, string, Promise<Lizard>] would be {}[] (that is, roughly, an Object[]).

Define derived type array

I am trying to initialize a derived type using a parameter declaration. When I compile, I get the following error
Element in INTEGER(4) array constructor at (1) is CHARACTER(1).
User defined kinds values ip and dp are found in fasst_global. They are:
integer,parameter:: ip = selected_int_kind(8)
integer,parameter:: dp = selected_real_kind(15,307)
I have tried using 1_ip instead of 1 as the first element and it made no difference. What am I doing wrong?
module fasst_derived_types
use fasst_global
implicit none
type fasst_default_soil
integer(ip):: sid
character(len=2):: ssname
real(dp):: dens,pors,ssemis,ssalb,shc,smin,smax,salpha,svgn
real(dp):: sspheat,sorgan,spsand,spsilt,spclay,spgravel
end type fasst_default_soil
type(fasst_default_soil),parameter:: fasst_soil(1) = fasst_default_soil( &
(/1, 'GW',1.947_dp,0.293_dp, 0.92_dp,0.40_dp,1.1197e-2_dp, &
0.01_dp,0.293_dp,22.6125_dp, 3.45_dp, 820.0_dp, &
0.0_dp, 5.0_dp, 2.0_dp, 2.0_dp,91.0_dp/))
end module fasst_derived_types
You are trying to use two constructors here:
an array constructor;
a structure constructor.
You have the correct syntax for each, but you are using them incorrectly.
The array constructor (/.../) is to construct an array. But you want an array of derived type values (well, one value) rather than an array as the component for a single derived type value. The syntax error comes from attempting to create an array with various different/incompatible types.
So, instead you want
type(fasst_default_soil),parameter:: fasst_soil(1) = (/fasst_default_soil(1_ip,'GW', ...)/)
Or, as you just want a single element array you don't even need to construct that array of derived types.

how to get array of properties from array of objects in matlab

I am using an array of objects in my program, and each object has several attributes. I want to be able to extract separate arrays from this array of objects, one array for each attribute.
here is a snippet of the relevant code:
dailyDataMatrix(m,n)=dailyData('',''); %creates an mxn array of dailyData objects
for i=1:m
for j=1:n
dailyDataMatrix(i,j)=dailyData(datainput1, datainput2)%dailyData constructor, sets attributes
end
end
dailyDataMatrix.attribute
But when I try to access a certain attribute as in the code above, say of type string, I get a strange result. Instead of getting an array of strings, I get something else. When I try to print it, rather than printing an array, it prints a series of individual values
ans = 'string1'
ans = 'string2'
...
When I try to call
size(dailyDataMatrix.attribute)
className = class(dailyDataMatrix.attribute)
I get
"error using size: too many input arguments" and
"error using class: The CLASS function must be called from a class constructor."
However, when I write this as
thing=dailyDataMatrix.attribute
className = class(thing)
size(thing)
I get the response
classname = 'double' and size = 1x1.
Why is this not returning an array the same size as dailyDataMatrix? And an aside question is why the two different ways of writing the code above give different results? and how can I get the result that I want?
Thanks,
Paul
You can capture all the outputs using a cell array or using square brackets if the types are same. For regular array when all values are of same type use,
thing=[dailyDataMatrix.attribute];
If the types are different you can use
thing = cell(1,N); % N is the number of elements in dailyDataMatrix
[thing{:}] = dailyDataMatrix.attribute;

Initialize Array to Blank custom type OCAML

ive set up a custom data type
type vector = {a:float;b:float};
and i want to Initialize an array of type vector but containing nothing, just an empty array of length x.
the following
let vecarr = Array.create !max_seq_length {a=0.0;b=0.0}
makes the array init to {a=0;b=0} , and leaving that as blank gives me errors. Is what im trying to do even possible?
You can not have an uninitialized array in OCaml. But look at it this way: you will never have a hard-to-reproduce bug in your program caused by uninitialized values.
If the values you eventually want to place in your array are not available yet, maybe you are creating the array too early? Consider using Array.init to create it at the exact moment the necessary inputs are available, without having to create it earlier and leaving it temporarily uninitialized.
The function Array.init takes in argument a function that it uses to compute the initial value of each cell.
How can you have nothing? When you retrieve an element of the newly-initialized array, you must get something, right? What do you expect to get?
If you want to be able to express the ability of a value to be either invalid or some value of some type, then you could use the option type, whose values are either None, or Some value:
let vecarr : vector option array = Array.create !max_seq_length None
match vecarr.(42) with
None -> doSomething
| Some x -> doSomethingElse
You can initialize and 'a array by using an empty array, i.e., [||]. Executing:
let a = [||];;
evaluates to:
val a : 'a array = [||]
which you can then append to. It has length 0, so you can't set anything, but for academic purposes, this can be helpful.

Creating an Array2D in F# (VS2010 Beta 1)

Consider the following code fragment in VS2010 Beta 1:
let array = Array2D.zeroCreate 1000 500
This produces an error, namely:
error FS0030: Value restriction. The value 'array' has been inferred to have
generic type val array : '_a [,]
Either define 'array' as a simple data term, make it a function with explicit
arguments or, if you do not intend for it to be generic, add a type annotation.
Can I explicitly set the type (in my case a grid of string)?
You can explicitly specify the type like this:
let array : string [,] = Array2D.zeroCreate 1000 500
For further information on the value restriction you might want to take a look at this F#-Wiki page.
You can also use init to create an array though it might be slower.
let array = Array2D.init 1000 500 (fun _ _ -> "")
Zeroing out an array is usually not seen in functional programming. It's much more common to pass an initilization function to init and just create the array with the values you want.
To create a 2-dimensional array containing empty strings:
let array = Array2D.create 1000 500 ""

Resources