CoffeeScript - transform object into a matrix using comprehensions - coffeescript

I have an object of the form: {a: [1,2,3,4], b: [5,6,7,8]} and I want to transform it, using comprehensions, into an array of arrays of 3 items:
[
['a', 0, 1], ['a', 1, 2], ['a', 2, 3], ['a', 3, 4],
['b', 0, 5], ['b', 1, 6], ['b', 2, 7], ['b', 3, 8]
]
I tried this ( [x,y,v] for v, y in h for x, h of obj ) but it gives an array of two elements of 4 elements:
[
[ [], [], [], [] ],
[ [], [], [], [] ]
]
How can I skip the array of the second level?

It's easier to see when you split out the two comprehensions:
result = for x, h of obj
for v, y in h
[x,y,v]
Your result will be 2 levels deep, as each comprehension is returning an array.
First depth array will have one array for each element in your object.
Each of these arrays will contain the results for one key in your object
Best way around this is to push each of the 3 element arrays you want into a separate array.
result = []
for x, h of obj
for v, y in h
result.push [x,y,v]
Alternatively with the compact formatting:
result = []
result.push [x,y,v] for v, y in h for x, h of obj
If you already have a utility library like lodash, you could use the flatten method. But this would involve another iteration over your arrays, so not practical if performance matters and you have very large datasets

Related

How to perform sum operation to 2X2 matrix in Flutter/dart

in my dart code, I have a 2X2 matrix, like this
List<List<int>> a =
[
[10, 2],
[2, 2],
];
List<List<int>> b =
[
[1, 2],
[2, 2],
];
so, I want to perform sum operation to these two matrices without using any predefined function.
the result that I want, like this
List<List<int>> c =
[
[11, 4], // 10+1 2+2
[4, 4], // 2+2 2+2
];
this is the general answer to the sum 2D matrix
List<List<int>> c=[];
int minSize=a.length>b.length?b.length:a.length;
for(int i=0;i<minSize;i++){
List<int> temp=[];
int minListSize=a[i].length>b[i].length?b[i].length:a[i].length;
for(int j=0;j<minListSize;j++){
temp.add(a[i][j]+b[i][j]);
}
c.add(temp);
}
Well, if you know beforehand they are both 2x2 matrices, then this straightforward way is a trivial solution, which I think even beats every other solution in terms of performance.
List<List<int>> c =
[
[a[0][0] + b [0][0], a[0][1] + b [0][1]],
[a[1][0] + b [1][0], a[1][1] + b [1][1]]
];

Basic Matlab for loop

A=2;
for x=0:2:4
A=[A, A*x];
end
A
I'd appreciate any help! The for loop condition as well as the 3rd line and how they work together I can't quite piece together
So, here comes the walktrough.
A = 2;
A is an array of length 1, with 2 as the only element.
for x = 0:2:4
Have a look at the Examples section of the for help. You create an "iteration variable" x, which iterates through an array with the values [0, 2, 4]. See also the Examples section of the : operator help.
A = [A, A*x];
Concatenate array A with the value of A*x (multiplying an array with a scalar results in an array of the same length, in which each element is multiplied by the given scalar), and re-assign the result to A. See also the help on Concatenating Matrices.
Initially, A = [2].
For x = 0: A = [[2], [2] * 0], i.e. A = [2, 0].
For x = 2: A = [[2, 0], [2, 0] * 2], i.e. A = [2, 0, 4, 0].
For x = 4: A = [[2, 0, 4, 0], [2, 0, 4, 0] * 4], i.e. A = [2, 0, 4, 0, 8, 0, 16, 0].
end
End of for loop.
A
Output content of A by implicitly calling the display function by omitting the semicolon at the end of the line, see here for explanation.

Copy to and delete from matrices

Not sure even if this question stands valid. But better ask.
Suppose we have two matrices in MATLAB of size (n,1) and (m,1) and we want to copy certain rows from matrix A to matrix B on a condition.
e.g. if value A(i,1) is less or equal to X
And later delete those rows from source matrix i.e. matrix A
Example:
A = [1, 2, 3, 4, 5, 6]
B = [8, 9]
copy all values which are less than or equal to 4 from A to B, and delete from A
Matrices becomes
A = [5, 6]
B = [8, 9, 1, 2, 3, 4]
You can use a logical matrix to identify the items:
mask = (A <= 4);
B = [B A(mask)];
A(mask) = [];

How to to generate all possible k-element arrays from n-element array in Swift

I want to create all possible k-element arrays from n-element array. k may be bigger or smaller than n. The elements in the output array don't have to be unique.
For example:
given this array
let a = [1,2]
the function given a desired size 3, should return :
[1,1,1]
[2,1,1]
[1,2,1]
[1,1,2]
[2,2,1]
[2,1,2]
[1,2,2]
[2,2,2]
example 2
given this array
let b = [[0,1], [2,3]]
the function given a desired size 3, should return :
[[0,1], [0,1], [0,1]]
[[2,3], [0,1], [0,1]]
[[0,1], [2,3], [0,1]]
[[0,1], [0,1], [2,3]]
[[2,3], [2,3], [0,1]]
[[2,3], [0,1], [2,3]]
[[0,1], [2,3], [2,3]]
[[2,3], [2,3], [2,3]]
How to do that in Swift?
So you want all k-tuples with elements from a given set. This can be done recursively
by taking all elements from the set as the first tuple element and combining that
with all (k-1) tuples:
func allTupelsFrom<T>(elements: [T], withLength k : UInt,
combinedWith prefix : [T] = []) -> [[T]] {
if k == 0 {
return [prefix]
}
var result : [[T]] = []
for e in elements {
result += allTupelsFrom(elements, withLength: k-1, combinedWith: prefix + [e])
}
return result
}
Examples:
let result1 = allTupelsFrom([1, 2], withLength: 3)
println(result1)
// [[1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2]]
let result2 = allTupelsFrom(["a", "b", "c", "d"], withLength: 4)
println(result2)
// [[a, a, a, a], [a, a, a, b], ... , [d, d, d, c], [d, d, d, d]]

Append values to several cells in cell array

Imagine I have a cell array
A = {0, 1 ,2, 3, ...}
and indice vector
I = [0, 1, 0, 1, 0, ...]
and values
V = [2, 3]
and I want something like
A{I} = [A{I}; V]' = {0, [1 2], 2, [3 3], ....};
That is, I want to append several values to some cells of a cell array at once. How would I do that most elegantly/efficiently? :)
You can use cellfun
A(I==1) = cellfun( #(x,y) [x y], A(I==1), num2cell(V), 'UniformOutput', 0 );
Note the usage of regular subscripting (using (), rather than {}) to index the chosen cell elements using I==1. Also note that V is passed as a cell array (using num2cell) and not as a regular array.