Accessing values in Swift 1.2 multi-dimensional arrays - arrays

Apologies in advance. I am a newbie learning Swift and have some working experience with C and Ruby. I'm venturing into Swift.
I'm trying to create and access a multi-dimensional array as follows:
var arrayTest: [[(Int, Int, Int, String)]] = []
arrayTest.append([(1, 2, 3, "Test")])
arrayTest.append([(4, 5, 6, "Test1")])
This seems to work fine in Playground. But trying to access as follows does not work:
println(arrayTest[0][0])
I looked at several entries and the Apple Swift docs. But I was still unable to figure this out.
Any insights appreciated. Thank you.

Looks like you're trying to make an array of tuples but have an extra set of brackets, meaning you're making an array of arrays of tuples. Also, you append a tuple without putting it in parenthesis. Try:
var arrayTest: [(Int, Int, Int, String)] = []
arrayTest.append(1, 2, 3, "Test")
arrayTest.append(4, 5, 6, "Test1")
You would access an element a little differently. Square brackets return the tuple in arrayTest, and the period returns an element of the tuple.
println(arrayTest[0].0)
This would return 1 (the 0th element of the 0th tuple).

Related

Can't add array to Matlab dictionary

I'm trying to add an array to a dictionary in Matlab.
In Python it's very simple, just make a dictionary and for a key assign an array, but in Matlab I get this error:
Error using () Dimensions of the key and value must be compatible, or the value must be scalar.
Basically all I want to do is:
d = dictionary
arr = [1 0.2 7 0.3]
d('key') = arr
But it doesn't work and I don't understand what I am supposed to do.
According to the documentation:
Individual elements of values must be scalars of the same data type. If values need to be heterogeneous or nonscalar, use a cell array.
So, to store an array under a key, use:
d("key") = {arr};
% create an array
array = [1, 2, 3];
% create a dictionary
dictionary = containers.Map;
% add the array to the dictionary
dictionary('array') = array;
I found this alternative by accident looking for a matlab reddit or discord server. And I basically found an amazing AI assistant website https://gistlib.com/matlab/add-array-to-dictionary-in-matlab.
So this solves this mystery.

Julia Quick way to initialise an empty array that's the same size as another?

I have an array
array1 = Array{Int,2}(undef, 2, 3)
Is there a way to quickly make a new array that's the same size as the first one? E.g. something like
array2 = Array{Int,2}(undef, size(array1))
current I have to do this which is pretty cumbersome, and even worse for higher dimension arrays
array2 = Array{Int,2}(undef, size(array1)[1], size(array1)[2])
What you're looking for is similar(array1).
You can even change up the array type by passing in a type, e.g.
similar(array1, Float64)
similar(array1, Int64)
Using similar is a great solution. But the reason your original attempt doesn't work is the number 2 in the type parameter signature: Array{Int, 2}. The number 2 specifies that the array must have 2 dimensions. If you remove it you can have exactly as many dimensions as you like:
julia> a = rand(2,4,3,2);
julia> b = Array{Int}(undef, size(a));
julia> size(b)
(2, 4, 3, 2)
This works for other array constructors too:
zeros(size(a))
ones(size(a))
fill(5, size(a))
# etc.

How to merge 2 arrays of equal length into a single dictionary with key:value pairs in Godot?

I have been trying to randomize the values in an ordered array (ex:[0,1,2,3]) in Godot. There is supposed to be a shuffle() method for arrays, but it seems to be broken and always returns "null". I have found a workaround that uses a Fisher-Yates shuffle, but the resulting array is considered "unsorted" by the engine, and therefore when I try to use methods such as bsearch() to find a value by it's position, the results are unreliable at best.
My solution was to create a dictionary, comprised of an array containing the random values I have obtained, merged with a second array of equal length with (sorted) numbers (in numerical order) which I can then use as keys to access specific array positions when needed.
Question made simple...
In GDScript, how would you take 2 arrays..
ex: ARRAY1 = [0,1,2,3]
ARRAY2 = [a,b,c,d]
..and merge them to form a dictionary that looks like this:
MergedDictionary = {0:a, 1:b, 2:c, 3:d}
Any help would be greatly appreciated.
Godot does not support "zip" methodology for merging arrays such as Python does, so I am stuck merging them manually. However... there is little to no documentation about how to do this in GDScript, despite my many hours of searching.
Try this:
var a = [1, 2, 3]
var b = ["a", "b", "c"]
var c = {}
if a.size() == b.size():
var i = 0
for element in a:
c[element] = b[i]
i += 1
print("Dictionary c: ", c)
If you want to add elements to a dictionary, you can assign values to the keys like existing keys.

How do I to lose the results of running python in the form of an array (ex: array ([1, 2, ..., 6, 7, 8])

example array :
[array([0.84312789, 0.75129959, 0.88778234, ..., 0.96588262, 0.98853215,
0.52034823])]
How do I know the value of (...) in the array list ?
thank you.
You are creating a list with an array in it, so your array is accessible with [0].
You can access elements in an array the same way you access list elements. You also have to supply a character code to the array module which describes the type of elements in the array.
example
from array import array
# 'd' describes double
x = array('d', [0.84312789, 0.75129959, 0.88778234, 0.96588262, 0.98853215, 0.52034823])
print(x[0][0])
prints
0.84312789

Using arrays in Swift

Just started using Swift and I'm getting pissed at a few elements. First is that most standard stuff are structs rather than objects, which means they're passed in as values rather than pointers as I'm used to. The other thing is that using the optional element system is really annoying.
If I am trying to declare an array without putting anything in it, I declare it like this:
var theArray : [Int]
In order to put anything in it, I would declare it like this:
var theArray : [Int]?
Then add objects as follows:
theArray[someIndex] = someInt
//or
theArray.append(someInt)
However, I get an error. In Java, I could have just initialized an array with a length, which would have given me an a fixed-size array with all 0's.
The problem, summarized in a sentence, is adding elements to Swift arrays that have been initialized without values. How do you do this?
In order to initialize an empty array use:
var theArray : [Int] = []
then add elements by using append method. What you currently did is that you just declared it in the first case non optional and in the second case as an optional variable typed as int array without initializing it.
If you want the array to contain Int types, of course there are many ways to declare that, based on implicit or explicit type inference. These are all valid declarations of an array containing Int types
var array1 = [Int]()
var array2: [Int] = []
var array3 = Array<Int>()
var array4: Array<Int> = []
If you want an array of a certain size, with the values initialized to a certain value you can use, in this example you'll get an Array<Int> with 5 elements, all initialised to 0
var array = Array(count: 5, repeatedValue: Int(0))
Here you go:
var theArray = Array(count:[the length you want], repeatedValue:Int(0))
This will replicate the Java behaviour.

Resources