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

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{:}

Related

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]

Find all unique n-combinations of n-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

Extract row or column from a 2D array to a 2D array [duplicate]

This question already has an answer here:
Vector multiplication using MATMUL in Fortran
(1 answer)
Closed 6 years ago.
in matlab I am used to write something like this
A = [1,2;3,4]
B = A(:,1)
So I extract the first column of matrix A and store it in matrix B, which is just a vector or a 2x1 Matrix. However I can't do this in Fortran, since it regards A(:,1) as an one dimensional array and thus gives me an error if I want to assign this to a "matrix" B of size 2x1.
This is an minimal example in Fortran:
program test
implicit none
double complex, dimension(:,:), allocatable :: A, B
allocate(A(2,2), B(2,1))
A = transpose(reshape((/ 1, 2, 3, 4/), shape(A)))
B = A(:,1) !gives error that shape mismatch
end program test
Since I don't want to treat vectors separately in my algorithm, how can I achieve Matlab like behaviour?
Try
B = A(:, 1:1)
Or you should also be able to do this:
B(:,1) = A(:,1)
Either should work.

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)

Vectors and dynamic arrays in D

I was thinking that dynamic arrays were a replacement for vectors in D, but it seems they have no remove function (only associative arrays do) which is rather a limitation for a vector so I'm wondering if I've got that right. If a have an array like follows,
uint[] a;
a.length = 3;
a[0] = 1;
a[1] = 2;
a[2] = 3;
Then the only way I've found to remove, say, the second element is,
a = a[0..1] ~ a[2];
But that doesn't seem right (but maybe only because I don't understand this all yet). So is there a vector and is there another way of removing an element from a dynamic array?
Thanks.
You could use std.algorithm.remove(), which works not only with arrays but with generic ranges. Example:
import std.algorithm;
void main() {
uint[] a = [1, 2, 3];
a = a.remove(1);
assert(a == [1, 3]);
}
In std.container there is an Array!T template, which appears to be much like std::vector from C++.
Array!int a = [0, 1, 2, 3];
a.linearRemove(a[1..3]);
assert(equal(a, [0, 3]));
Unfortunately it does not appear to have an individual remove method, although you could always use linearRemove with a singleton range.

Resources