Scala Multi Dimensional Array - arrays

I have the following two arrays
val a = Array(1,2)
val b = Array(0,a)
println(b(1)(0)) //'Any' does not take parameters
In the above example I can't index the value in the second array as '(0)' is not allowed. The expected result should print the value 1.
Is this possible?
Any help would be appreciated!!

The Multi-Dimensional Array is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns.
val a = Array(1, 2)
val b: Array[Any] = Array(0, a)
println(b(1)(0)) //'Any' does not take parameters. since b is not a valid Multi-Dimensional Array
val aa = Array(3, 4)
val c: Array[Array[Int]] = Array(aa, a) //Valid Multi-Dimensional Array
println(c(1)(0))

Related

how to compare two 1d arrays and output the indices where array 1 has the same scalar value as array 2

I'm trying to figure out how to take two 1d arrays, put it in a function so that every number in array 1 is compared to every number in array 2, and then the element numbers of the duplicate numbers found is displayed with respect to array 1
for example
array 1 = [12,16,36,72,82]
array 2= [16,53,72,12,40,71]
and the output would be
elements= 1 2 4
I'm new to Matlab so I don't currently have all the skills to make This work I'm trying to figure it out but I don't know what exactly to do.
it won't let me post the code because it doesn't make sense.
im not sure how to post is on her otherwise.
[Edit2]
Best approach
The unreliable approach below works for the example given by the OP, with the restriction discussed below. The best approach is using MATLAB's ismember and find functions:
array1 = [12,16,36,72,82];
array2 = [16,53,72,12,40,71];
idc = find( ismember(array1, array2 ) )
ismember(array1, array2) returns a logical array indicating which elements of array1 are contained in array2.
find(...) converts the logical array to indices.
[Edit1]
Alternative approach
An approach without ismember would be:
array1 = [12,16,36,72,82];
array2 = [16,53,72,12,40,71];
findFcn = #(X) find( array1(:) == X )';
idcs = arrayfun(findFcn, array2(:), 'UniformOutput', false );
idcs = unique([idcs{:}])
Explanation:
findFun(X) gives the index of the value of X in array1.
findFun(X) is called by arrayfun with X being equal to each element of array2 and arrayfun stores the return values of the calls to the cell array idcs.
Finally, idcs = unique([idcs{:}]); converts the cell array idc to a double array and removes repetitions.
[First answer]
Unreliable approach
You can use the intersect function:
array1 = [12,16,36,72,82];
array2 = [16,53,72,12,40,71];
[ ~, x1 ] = intersect( array1, array2 );
~ means that the first return value, which would be the intersection of array1 and array2, is discarded. x1 is then equal to the indices that the intersection's values have in array1.
If you also want to have the indices in array2 you could do
[ ~, x1, x2 ] = intersect( array1, array2 );
to store them in x2.
Note:
x1 and x2 only contain the indices of the first occurrence in array1 and array2, respectively.

Indexing 3D arrays with Numpy

I have an array in three dimensions (x, y, z) and an indexing vector. This vector has a size equal to the dimension x of the array. Its objective is to index a specific y bringing their respective z, i.e., the expected result has dimension (x, z).
I wrote a code that works as expected, but does anyone know if a Numpy function can replace the for loop and solve the problem more optimally?
arr = np.random.rand(100,5,2)
result = np.random.rand(100,2)
id = [np.random.randint(0, 5) for _ in range(100)]
for i in range(100):
result[i] = arr[i,id[i]]
You can achieve this with this piece of code:
import numpy as np
arr = np.random.randn(100, 5, 2)
ids = np.random.randint(0, 5, size=100)
res = arr[range(100), ids]
res.shape # (100, 2)

Is there any way to create dynamic array in scala? Means inserting values at run time?

var arr=Array.ofDim[Int](4,4)
arr(0)(0)(0)(0)=12
this is one way to insert elements in array.
but if i need to initialize size of array dynamically or at run time. How can we do it in scala?
Here
val n = StdIn.readInt
val m = StdIn.readInt
val arr = Array.ofDim[Int](n, m)
arr(5)(15) = 1
println(arr.deep.mkString("\n"))
I created 2-dimensional array with sizes known only in runtime (I entered 10 and 20).
Or maybe you need scala.collection.mutable.ArrayBuffer if you're going to change sizes.
ArrayBuffer[Int] is 1-dimensional, ArrayBuffer[ArrayBuffer[Int]] is 2-dimensional etc.
val arr: ArrayBuffer[ArrayBuffer[Int]] =
ArrayBuffer.fill(10)(ArrayBuffer.fill(20)(0))
arr(5)(15) = 1
println(arr.mkString("\n"))
println
arr(5) += 1
arr += ArrayBuffer.fill[Int](25)(0)
println(arr.mkString("\n"))

Adding another column to a numpy array

I have two arrays of the following dimensions:
a = (6,1) b = (6,4)
I wish to add array (a) as additional column to array (c).
Tried:
c= np.column_stack([b,a])
and get an error due to mismatch of dimensions.
Try:
c = np.concatenate((b,a), axis=1)
This assumes that a.shape = (6,1). If a.shape = (6,) then you can do:
c = np.concatenate((b,a.reshape((6,1))), axis=1)

Compare size of 2 Arrays in Scala

If I have 2 arrays, how do I compare them and return the size of the smallest one? Here is what I mean:
val a = Array(1,2,3)
val b = Array(1,2,3,4)
is there some operator that I could call to compare the sizes of both and return 3, since Array a is smaller and has 3 elements?
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> val b = Array(1,2,3,4)
b: Array[Int] = Array(1, 2, 3, 4)
scala> math.min(a.length, b.length)
res0: Int = 3
A more generic approach, assuming you want to compare Sequences of the same type.
def getSmallerCollectionSize[T](a:Seq[T],b:Seq[T]) =
Math.min(a.size, b.size)
scala> val a = Array(1,2,3)
a:Array[Int] = Array(1,2,3)
scala> val b = Array(1,2,3,4)
b:Array[Int] = Array(1,2,3,4)
scala> a.size min b.size
res0: Int = 3
The size method gets the size of the Array and min is a comparator function between the two sizes. As with any function taking two parameters, you can call it by putting the function name between the parameters.
min is doing an implicit conversion (a set of methods that Scala tries to apply when it encounters an object of the wrong type being used) from Int to RichInt.

Resources