Find all unique n-combinations of n-arrays - arrays

Assuming I have n arrays of the same length m.
How can I find all fully unique n-combinations of the elements of those arrays? Either language or pseudo code is totally fine.
Example with n=2 and m=2:
arr1 = {a, b}
arr2 = {c, d}
returnUniqueCombinations();
//Should return
//a,c
//a,d
//b,c
//b,d

Related

Matlab: How to do `repelem` where each element is a matrix?

Suppose I have m-by-n matrices A, B, C arranged in an m-by-n-by-3 tensor P:
P = cat(3, A, B, C);
I now want to make a new tensor where each matrix is repeated K times, making the third dimension size 3K. That is, if K=2 then I want to build the tensor
Q = cat(3, A, A, B, B, C, C);
Is there a nice builtin way to achieve this, or do I need to write a loop for it? Preferably as fast or faster than the manual way.
If A, B, C were scalars I could have used repelem, but it does not work the way I want for matrices. repmat can be used to build
cat(3, A, B, C, A, B, C)
but that is not what I am after either.
As noted by #Cris Luengo, repelem(P, 1, 1, k) will actually do what you want (in spite of what the MATLAB documentation says), but I can think of two other ways to achieve this.
First, you could use repmat to duplicate the tensor k times in the second dimension and then reshape:
Q = reshape(repmat(P, 1, k, 1), m, n, []);
Second, you could use repelem to give you the indices of the third dimension to construct Q from:
Q = P(:, :, repelem(1:size(P,3), k));

Defining an array(C) in Julia by appending array B to array A without changing the values of the array A

I have two arrays A and B. Let's say A=[1,2,3] and B=[4,5,6]. I want to define a third array C = append!(A,B). The problem is this also changes A to be A=[1,2,3,4,5,6]. How to avoid this problem?
append! pushes the elements of the second collection to the first one, modifying it, to just concatenate them, use vcat:
C = vcat(A, B)
Or you can use ; to build a new array from the contents of A and B:
C = [A ; B]
You could try this:
A=[1,2,3]
B=[4,5,6]
C=A
append!(C,B)
Then C = [1,2,3,4,5,6] and B = [4,5,6]. But unfortunately, A = [1,2,3,4,5,6].
The problem is C=A, which makes A and C the same in every way, except for the variable name. Unfortunately, changes to C then also change A, because they occupy the same area of memory, they have the same address.
What you have to do is to copy A to C, before appending B. Then C and A have the same data, before B is appended, but they are separate things. Changing C no longer changes A.
A = [1,2,3]
B = [4,5,6]
C=copy(A)
append!(C,B)
Then C = [1,2,3,4,5,6] and B = [4,5,6]. And, A = [1,2,3].

Return a map and store the result in diferent variables

In JavaScript I can store the values of array into variables like this:
[a, b, c] = [1, 2, 3]
I created a variable function( it return a map with the same number of arguments), I would like to know if Go has a shortcut like JavaScript
Based on the suggestion from comments ,I created a small example for your scenario:
package main
import (
"fmt"
)
func main() {
myArray := []int{1, 2, 3}
fmt.Println(myArray)
a, b, c := myArray[0], myArray[1], myArray[2]
fmt.Println(a, b, c)
}
Output:
[1 2 3]
1 2 3
Javascript's destructuring assignment syntax is not available in Go, however Go's syntax is sometimes concise too, depending what your input looks like.
Declaring and initializing several variables in a single LOC is straightforward, and it works even with variables of different types:
a, b, c := 42, "hello", 5.0
Source. Playground.
You can assign values to existing variables as well:
a, b, c = 42, "hello", 5.0
Playground.
If your input data is a slice s, then per #Gopher's answer the code will look like:
a, b, c := s[0], s[1], s[2]

creating 2 dimensional array of unspecified (varying) size

Newbie question: I want to dynamically create an integer 2D array M[i,j], whose sizes (in both dimensions) are unknown beforehand. Moreover, for each index i, the size of the i-th row may vary.
Question 1: How do I declare such an array (do I even have to)? I have tried Array[], Array(Int64,1...), and Array((Int,Int),0)as in this hint and others.
Question 2: once created, how to I populate the array in a smart and concise way? Say my i-th row is suppose to be equal to a given 1-dimensional B, I would like to write
A[i] = B
or
A[i,:] = B
or even
A[i,1:n] = B
where n is the size of B. All of these give me a BoundsError(). Slicemight do the trick, but I cannot make it agree with my declaration.
You don't want a 2D array here, because in a 2D array all rows are of the same size. Instead, you want a vector-of-vectors. For example:
A = Array(Vector{Int}, 5)
A[1] = rand(1:10, 3)
A[2] = rand(1:100, 22)
If you inspect A, you'll see something like this:
julia> A
5-element Array{Array{Int64,1},1}:
[5,7,7]
[1,63,40,86,61,39,98,5,68,97 … 78,49,44,89,48,63,90,90,86,83]
#undef
#undef
#undef
Another great tool is to use a comprehension:
julia> A = Vector{Int}[ [1:m] for m = 1:5]
5-element Array{Array{Int64,1},1}:
[1]
[1,2]
[1,2,3]
[1,2,3,4]
[1,2,3,4,5]
The main thing you'll want to be careful about is that each element of A is a reference to a vector; if you assign
A[1] = b
A[2] = b
then any change to b will affect both A[1] and A[2]. If you don't want that, use
A[1] = copy(b)
A[2] = copy(b)

Is there anything like deal() for normal MATLAB arrays? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I do multiple assignment in MATLAB?
When dealing with cell arrays, I can use the deal() function to assign cells to output variables, such as:
[a, b, c] = deal(myCell{:});
or just:
[a, b, c] = myCell{:};
I would like to do the same thing for a simple array, such as:
myArray = [1, 2, 3];
[a, b, c] = deal(myArray(:));
But this doesn't work. What's the alternative?
One option is to convert your array to a cell array first using NUM2CELL:
myArray = [1, 2, 3];
cArray = num2cell(myArray);
[a, b, c] = cArray{:};
As you note, you don't even need to use DEAL to distribute the cell contents.
Not terribly pretty, but:
myArray = 1:3;
c = arrayfun(#(x) x, myArray , 'UniformOutput', false);
c{:}

Resources