Can't add array to Matlab dictionary - arrays

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.

Related

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.

Accessing values in Swift 1.2 multi-dimensional 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).

Accessing n-dimensional array in R using a function of vector of indexes

my program in R creates an n-dimensional array.
PVALUES = array(0, dim=dimensions)
where dimensions = c(x,y,z, ... )
The dimensions will depend on a particular input. So, I want to create a general-purpose code that will:
Store a particular element in the array
Read a particular element from the array
From reading this site I learned how to do #2 - read an element from the array
ll=list(x,y,z, ...)
element_xyz = do.call(`[`, c(list(PVALUES), ll))
Please help me solving #1, that is storing an element to the n-dimensional array.
Let me rephrase my question
Suppose I have a 4-dimensional array. I can store a value and read a value from this array:
PVALUES[1,1,1,1] = 43 #set a value
data = PVALUES[1,1,1,1] #use a value
How can I perform the same operations using a function of a vector of indexes:
indexes = c(1,1,1,1)
set(PVALUES, indexes) = 43
data = get(PVALUES, indexes) ?
Thank you
Thanks for helpful response.
I will use the following solution:
PVALUES = array(0, dim=dimensions) #Create an n-dimensional array
dimensions = c(x,y,z,...,n)
Set a value to PVALUES[x,y,z,...,n]:
y=c(x,y,z,...,n)
PVALUES[t(y)]=26
Reading a value from PVALUES[x,y,z,...,n]:
y=c(x,y,z,...,n)
data=PVALUES[t(y)]
The indexing of arrays can be done with matrices having the same number of columns as there are dimensions:
# Assignment with "[<-"
newvals <- matrix( c( x,y,z,vals), ncol=4)
PVALUES[ newvals[ ,-4] ] <- vals
# Reading values with "["
PVALUES[ newvals[ ,-4] ]

MATLAB cell2mat unique error cell array

I created a cell Array like this
A{1} = {'aa','b','d','aa'};
A{2} = {'c','d','aa'};
A{3} = {'bb','aa','bb','aa'};
now I wanna find the unique words
b=cell2mat(A)
unique(b)
but I get this Error: Error using cell2mat (line 52) Cannot support cell arrays containing cell arrays or objects.
I'm fairly new to matlab. Am I doing something wrong here?
A{1} = {'aa','b','d','aa'};
A{2} = {'c','d','aa'};
A{3} = {'bb','aa','bb','aa'};
unique([A{:}])
There you go. The {:}, (:) and [] operators are very useful in MATLAB. Get comfortable using them.

Sorting a cell array of matrices based on numerical value in each matrix?

I am working on a coding project and ran into a roadblock. I have a cell array of 1x3 matrices. (1,1) encodes the value to sort by, (1,2) and (1,3) encode coordinates that i need for reference later. Is there any way to sort the cell array by the (1,1) values in each matrix within the larger cell array?
CombList = {[1,1,1], [5,1,2];
[4,1,3], [3,1,2];
[2,1,4], [2,1,3]};
I would like to sort by the first values in each matrix within the cell array. Ideally, it would return:
CombList = [1,1,1], [2,1,3];
[2,1,4], [3,1,2];
[4,1,3], [5,1,2]};
...once sorted:)
Thank you!
I believe the following should work. The result will be a numeric array, hope that will work for you.
CombList = {[1,1,1], [5,1,2];
[4,1,3], [3,1,2];
[2,1,4], [2,1,3]}
CombMat = cell2mat(CombList);
CombMat(:, 1:3) = sortrows(CombMat(:, 1:3));
CombMat(:, 4:6) = sortrows(CombMat(:, 4:6));
You can use mat2cell to get convert it back to a cell array, like this:
CombCell = mat2cell(CombMat, [1 1 1], [3 3])
Zany one-liner based on sortrows:
CombList = reshape(mat2cell(sortrows(cell2mat(reshape(CombList,[],1))),ones(numel(CombList),1),numel(CombList{1})),2,[]).';

Resources