how do I concatenate the results of a concatenated array? - group-by

I have two matrices (dfs):
A = [1 2 3 4
5 6 7 8
9 10 11 12]
and B = [1, 2, 3]
and I want matrix C to be repeating each row in A, B times. for example, first row, 1,2,3,4 needs to be repeated once, second row: 5,6,7,8 twice and last row three times:
C = [1 2 3 4
5 6 7 8
5 6 7 8
9 10 11 12
9 10 11 12
9 10 11 12]
my code
for i in range(0,2401):
g = pd.concat([df1.iloc[[i]]]*z[i], ignore_index=True)
partially does this, except only gives me the 3 times last row part, I need to concatenate each concatenation.
below gives me what I want but its not clean, i.e. indices are not ignored and messy.
result = []
for i in range(0,2401):
g = pd.concat([df1.iloc[[i]]]*z[i], ignore_index=True)
result.append(g)

If you write your matrices like:
A = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
B = [1, 2, 3]
C = []
You can iterate through B and add to C like:
for i in range(len(B)):
index = 0
while index < B[i]:
C.append(A[i])
index += 1
Which has an output:
[[1, 2, 3, 4],
[5, 6, 7, 8],
[5, 6, 7, 8],
[9, 10, 11, 12],
[9, 10, 11, 12],
[9, 10, 11, 12]]
I hope this helps!

Related

How to check if a list of lists contains any of the elements from another list

I'm trying to make a sample lottery checker.
I'm using python.
x = [1,2,3,4,5,
y = [[1,2,3,4,5,6] # 6 numbers hit
,[1,2,3,4,6,7] # 5 numbers hit
,[2,3,4,6,7,8] # 4 numbers hit
,[4,5,6,7,8,9] # 3 numbers hit
,[1,2,7,8,9,10] # 2 numbers hit
,[4,7,8,9,10,11] # 1 number hit
,[7,8,9,10,11,12]]
output: (including the number of hits)
[1,2,3,4,5,6] 6 number hit
[1,2,3,4,6,7] 5 numbers hit
[2,3,4,6,7,8] 4 numbers hit
[4,5,6,7,8,9] 3 numbers hit
[1,2,7,8,9,10] 2 numbers hit
[4,7,8,9,10,11] 1 number hit
I tried using the any() function but only returned true or false.
please help.
Data:
x = [1,2,3,4,5,6]
y = [[1,2,3,4,5,6] # 6 numbers hit
,[1,2,3,4,6,7] # 5 numbers hit
,[2,3,4,6,7,8] # 4 numbers hit
,[4,5,6,7,8,9] # 3 numbers hit
,[1,2,7,8,9,10] # 2 numbers hit
,[4,7,8,9,10,11] # 1 number hit
,[7,8,9,10,11,12]]
Code:
for ticket in y:
print(ticket)
count = 0
for item in x:
if item in ticket:
count += 1
print(count, " numbers hit!")
Output:
[1, 2, 3, 4, 5, 6]
6 numbers hit!
[1, 2, 3, 4, 6, 7]
5 numbers hit!
[2, 3, 4, 6, 7, 8]
4 numbers hit!
[4, 5, 6, 7, 8, 9]
3 numbers hit!
[1, 2, 7, 8, 9, 10]
2 numbers hit!
[4, 7, 8, 9, 10, 11]
1 numbers hit!
[7, 8, 9, 10, 11, 12]
0 numbers hit!

Expanding each element in a (2-by-2) matrix to a (3-by-2) block

I want to expand each element in a (2-by-2) matrix to a (3-by-2) block, using Python 3 --- with professional and elegant codes. Since I don't know the python codes, I will just describe the following in maths
X = # X is an 2-by-2 matrix.
1, 2
3, 4
d = (3,2) # d is the shape that each element in X should be expanded to.
Y = # Y is the result
1, 1, 2, 2
1, 1, 2, 2
1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4
3, 3, 4, 4
Not that every element in X is now an 3-by-2 block in Y. The position of the block in Y is the same as the position of the element in X.
Here is the MATLAB code
X = [1,2;3,4];
d = [3,2]
[row, column] = size(X);
a = num2cell(X);
b = cell(row, column);
[b{:}] = deal(ones(d));
Y = cell2mat(cellfun(#times,a,b,'UniformOutput',false));
I appreciate your help. Thanks in advance.
If you are okay with using NumPy module with Python, you can use numpy.kron -
np.kron(X,np.ones((3,2),dtype=int))
Sample run -
In [15]: import numpy as np
In [16]: X = np.arange(4).reshape(2,2)+1 # Create input array
In [17]: X
Out[17]:
array([[1, 2],
[3, 4]])
In [18]: np.kron(X,np.ones((3,2),dtype=int))
Out[18]:
array([[1, 1, 2, 2],
[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4],
[3, 3, 4, 4]])
In fact, this is a direct translation of how one would achieved the desired result in MATLAB in an elegant and professional way as well, as shown below -
>> X = [1,2;3 4]
X =
1 2
3 4
>> kron(X,ones(3,2))
ans =
1 1 2 2
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
3 3 4 4
Another way to do it with ndarray.repeat:
>>> X = np.arange(4).reshape(2,2)+1
>>> X.repeat(3, axis=0).repeat(2, axis=1)
array([[1, 1, 2, 2],
[1, 1, 2, 2],
[1, 1, 2, 2],
[3, 3, 4, 4],
[3, 3, 4, 4],
[3, 3, 4, 4]])

Extract elements from matrix

How can I extract the elements: [1,2,5,6], [3,4,7,8], [9,10,13,14], [11,12,15,16] ?
A = [1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12;
13, 14, 15, 16;];
I'm using octave.
Best regards, Chris.
If you need four matrices then use
out = mat2cell(A,[2 2], [2 2]);
If you need four vectors with values , then use
out = cellfun(#(x)(reshape(x,1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);
output will be
out{:,:}
ans =
1 5 2 6
ans =
9 13 10 14
ans =
3 7 4 8
ans =
11 15 12 16
Thanks, to Joe Serrano ,If you need the value in each of the four vectors in same order use,
out = cellfun(#(x)(reshape(x',1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);
output will be
out{:,:}
ans =
1 2 5 6
ans =
9 10 13 14
ans =
3 4 7 8
ans =
11 12 15 16

MATLAB: Finding n-th smallest element in per row

I want to find the n-th smallest element for each row in a matrix.
Example:
n = 2
M = [1, 2, 3; 4, 5, 6; 7, 8 9]
Result = [2, 5, 8]
First sort the matrix by the second dimension (i.e. sort every row in ascending order):
n = 2
M = [1, 2, 3; 4, 5, 6; 7, 8 9]
M_SORTED = sort(M,2)
M_SORTED =
1 2 3
4 5 6
7 8 9
The n-th column of the matrix will contain the result:
RESULT = M_SORTED(:, n)
RESULT =
2
5
8

export Matrix with this format MATLAB

How to export any size matrix like
A=
1 2 3 4 5 6 ....9
3 6 7 8 9 9 ....4
...
6 7 7 4 4 5 ... 2
To a file that will contain that matrix where each value is separated by ',':
1, 2, 3, 4, 5, 6, ....,9
3, 6, 7, 8, 9, 9, ....,4
...
6, 7, 7, 4, 4, 5, ... ,2
Use DLMWRITE. Doing this:
>> A = [1 2 3 4; 5 6 7 8];
>> dlmwrite('file.csv', A);
writes a file with this:
1,2,3,4
5,6,7,8