Concatenating a variable number of arrays in Julia - arrays

Suppose there is a variable number of 2D arrays which I want to concatenate into a 3D array:
n = 10 # Number of arrays, can be changed to other integers
arrays = Dict()
for i in 1:n
arrays[i] = rand(2,2)
end
The syntax for concatenating arrays, as far as I know, is:
cat(arr1, arr2, arr3, ..., dims=3)
Since the number of arguments is variable, I can only think of the solution:
3d_array = arr1
for i in 2:n
3d_array = cat(3d_array, arrays[i])
end
But how do I concatenate it in the direction dims=3 with one line only, without for loops, etc.?

given the initial code:
n = 10 #random positive integer
arrays = Dict()
for i in 1:n
arrays[i] = rand(2,2)
end
here are some options:
using cat with splatting:
res1 = cat(values(arrays)...,dims=3) #values(dict) gives an iterable of all values stored
using reduce with cat:
res2 = reduce((x,y)->cat(x,y,dims=3),values(arrays)) #using anonymous function to pass kwargs
Im gonna guess and assume that you also want that the following equality holds true:
arrays[i] == res[:,:,i] # for i in 1:n
there is a problem here, as Dicts are unordered, you can check on the display:
julia> arrays
Dict{Any,Any} with 10 entries:
7 => [0.586479 0.280905; 0.805592 0.737151]
4 => [0.0214868 0.340997; 0.191425 0.271359]
9 => [0.060134 0.939555; 0.0896634 0.455099]
10 => [0.990368 0.214775; 0.224519 0.767086]
2 => [0.578315 0.109518; 0.794717 0.0584819]
3 => [0.106458 0.287653; 0.523525 0.277063]
5 => [0.372227 0.151974; 0.921043 0.238088]
8 => [0.690332 0.14813; 0.771126 0.320432]
⋮ => ⋮
How to solve this? changing the iterator:
cat with ordered splatting:
res3 = cat((arrays[i] for i in 1:n)...,dims=3) #using iterator syntax to return ordered values
reduce with ordered cat:
res4 = reduce((x,y)->cat(x,y,dims=3),(arrays[i] for i in 1:n))
at last, not asked, but my favorite, using broadcasting syntax to put those values on an prealocated array:
res5 = zeros(eltype(arrays[1]),2,2,n) #if you know the size beforehand
res5 = zeros(eltype(arrays[1]),size(arrays[1])...,n) #if you dont know
for i in 1:n
res5[:,:,i] .= arrays[i]
end

You use reduce. The syntax is
reduce((x,y) -> cat(x,y,dims = 3), arrays)

Related

Error in swift playground: Cannot assign value of type 'Int' to type '[Int]'

var number: [Int] = [1,2,3,4]
var newArray: [Int] = []
for i in 0...number.count-1{
newArray = number[i] * number[i+1]
}
print(newArray)
I want output like this: [1 * 2, 2 * 3, 3 * 4].
I just don't get it where is the problem...
var number: [Int] = [1,2,3,4]
let things = zip(number, number.dropFirst()).map(*)
Whenever you need to turn something like [1, 2, 3, 4] into pairs (1, 2), (2, 3) etc, then the AdjacentPairs method is useful - in Swift Algorithms package - https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/AdjacentPairs.swift
Or you can zip a collection with its dropFirst for the same result.
And whenever you need to turn an [A]s into an [B]s then map with a function that turns As into Bs. So in this example you want to turn an array of tuples of Int, like [(1,2), (2,3), (3,4)] into array of Int, like [2, 6, 12] by multiplying the 2 Ints together, so map with *
The benefit of writing it this way is you would avoid the issues with your array mutation, getting index values wrong, running off the ends of arrays etc, and it's often easier to read and think about if you express it without the indices and assignments.
The problem that the compiler flags you is that you assign a single Int value to an array of Int. The following line will resolve that immediate issue:
newArray.append(number[i] * number[i+1])
This should pass compilation but then create a runtime error at execution. The reason is that when you try to fetch number[i+1] when i == number.count-1, you actually fetch number[number.count]. This entry does not exist with 0-based indices. To get 3 sums out of 4 array entries, your loop should iterate 3 times:
for i in 0 ..< number.count-1 {
Or, if you prefer closed ranges:
for i in 0 ... number.count-2 {
A more Swifty way would be to use map, as #Dris suggested. The return type for map is implicitly given by the result of the multiplication, so you can write:
let newArray = number.indices.dropLast().map { i in
number[i] * number[i+1]
}
You can use map()
let numbers = [1,2,3,4]
let newArray = numbers.enumerated().map { $1 * numbers[($0 + 1) % numbers.count] }
May be you should not loop to count-1 but stop before and add result to array :
for i in 0..<number.count-1 {
newArray.append(number[i] * number[i+1])
}

Finding the common value in N number of arrays

I have n number of arrays and I want to work out if there is a common value in these arrays. If I knew the number of arrays I could do something like:
a = [1,2,3]
b = [2,4,5]
c = [2,6,7]
x = a & b & c
x == [2]
However, this isn't possible if you don't know the number of arrays. So far I've come up with this:
array_of_integers = [[1,2,3],[2,4,5]....]
values = []
array_of_integers.each_with_index do |array, index|
values = if index.zero?
array
else
values & array
end
end
# `values` will be an array of common values
However, this doesn't seem very efficient. Is there a better way?
However, this isn't possible if you don't know the number of arrays.
Actually, Enumerable#reduce can help with it:
[[1,2,3], [2,4,5], [2,6,7]].reduce(&:&) # => [2]
&:& looks interesting, but it's just:
[[1,2,3], [2,4,5], [2,6,7]].reduce { |memo, el| memo & el } # => [2]
Or it's also possible to do it as #Jagdeep suggested:
[[1,2,3], [2,4,5], [2,6,7]].reduce(:&) # => [2]

Determine Size of Multidimensional Array in Swift

I am new to Swift and am struggling to work out how to determine the size of a multidimensional array.
I can use the count function for single arrays, however when i create a matrix/multidimensional array, the output for the count call just gives a single value.
var a = [[1,2,3],[3,4,5]]
var c: Int
c = a.count
print(c)
2
The above matrix 'a' clearly has 2 rows and 3 columns, is there any way to output this correct size.
In Matlab this is a simple task with the following line of code,
a = [1,2,3;3,4,5]
size(a)
ans =
2 3
Is there a simple equivalent in Swift
I have looked high and low for a solution and cant seem to find exactly what i am after.
Thanks
- HB
Because 2D arrays in swift can have subarrays with different lengths. There is no "matrix" type.
let arr = [
[1,2,3,4,5],
[1,2,3],
[2,3,4,5],
]
So the concept of "rows" and "columns" does not exist. There's only count.
If you want to count all the elements in the subarrays, (in the above case, 12), you can flat map it and then count:
arr.flatMap { $0 }.count
If you are sure that your array is a matrix, you can do this:
let rows = arr.count
let columns = arr[0].count // 0 is an arbitrary value
You must ask the size of a specific row of your array to get column sizes :
print("\(a.count) \(a[0].count)")
If you are trying to find the length of 2D array which in this case the number of rows (or # of subarrays Ex.[1,2,3]) you may use this trick: # of total elements that can be found using:
a.flatMap { $0 }.count //a is the array name
over # of elements in one row using:
a[0].count //so elemints has to be equal in each subarray
so your code to get the length of 2D array with equal number of element in each subarray and store it in constant arrayLength is:
let arrayLength = (((a.flatMap { $0 }.count ) / (a[0].count))) //a is the array name

Counting matching elements in an array

Given two arrays of equal size, how can I find the number of matching elements disregarding the position?
For example:
[0,0,5] and [0,5,5] would return a match of 2 since there is one 0 and one 5 in common;
[1,0,0,3] and [0,0,1,4] would return a match of 3 since there are two matches of 0 and one match of 1;
[1,2,2,3] and [1,2,3,4] would return a match of 3.
I tried a number of ideas, but they all tend to get rather gnarly and convoluted. I'm guessing there is some nice Ruby idiom, or perhaps a regex that would be an elegant answer to this solution.
You can accomplish it with count:
a.count{|e| index = b.index(e) and b.delete_at index }
Demonstration
or with inject:
a.inject(0){|count, e| count + ((index = b.index(e) and b.delete_at index) ? 1 : 0)}
Demonstration
or with select and length (or it's alias – size):
a.select{|e| (index = b.index(e) and b.delete_at index)}.size
Demonstration
Results:
a, b = [0,0,5], [0,5,5] output: => 2;
a, b = [1,2,2,3], [1,2,3,4] output: => 3;
a, b = [1,0,0,3], [0,0,1,4] output => 3.
(arr1 & arr2).map { |i| [arr1.count(i), arr2.count(i)].min }.inject(0, &:+)
Here (arr1 & arr2) return list of uniq values that both arrays contain, arr.count(i) counts the number of items i in the array.
Another use for the mighty (and much needed) Array#difference, which I defined in my answer here. This method is similar to Array#-. The difference between the two methods is illustrated in the following example:
a = [1,2,3,4,3,2,4,2]
b = [2,3,4,4,4]
a - b #=> [1]
a.difference b #=> [1, 3, 2, 2]
For the present application:
def number_matches(a,b)
left_in_b = b
a.reduce(0) do |t,e|
if left_in_b.include?(e)
left_in_b = left_in_b.difference [e]
t+1
else
t
end
end
end
number_matches [0,0,5], [0,5,5] #=> 2
number_matches [1,0,0,3], [0,0,1,4] #=> 3
number_matches [1,0,0,3], [0,0,1,4] #=> 3
Using the multiset gem:
(Multiset.new(a) & Multiset.new(b)).size
Multiset is like Set, but allows duplicate values. & is the "set intersection" operator (return all things that are in both sets).
I don't think this is an ideal answer, because it's a bit complex, but...
def count(arr)
arr.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
end
def matches(a1, a2)
m = 0
a1_counts = count(a1)
a2_counts = count(a2)
a1_counts.each do |e, c|
m += [a1_counts, a2_counts].min
end
m
end
Basically, first write a method that creates a hash from an array of the number of times each element appears. Then, use those to sum up the smallest number of times each element appears in both arrays.

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] ]

Resources