Combine two matrices row by row - pytorch

I have two matrices of the size [18,28,28,5,5] and I want to merge them and get a matrix of the size [18,280,140] in the following sense. I take each square 5x5 from the first matrix and put a 5x5 square from the second matrix below it. That way I have 280 (28x5x2) rows and 140 columns (28x5).
Is there a fast way to do it? I've tried with for loops over 28 but it takes a lot of time.
Code example:
for row in range(28):
for col in range(28):
C[i,:,row*10:(row+1)*10,col*5:(col+1)*5] = torch.cat((torch.reshape(A[:,row,col,:],[18,5,5]),torch.reshape(B[:,row,col,:],[18,5,5])),dim=1)
I'm pretty sure that I can use unsqueeze instead of reshape but that's how I've done it.
Thanks!

Related

How to keep track of rows and columns of a matrix when turning it into a block diagonal one?

I am using scipy.linalg.block_diag function to create a block diagonal matrix from a given dataframe which has same number and label for its rows and columns. While the said function can create a block diagonal matrix from the given input, it is not keeping track of the name of the rows and columns of the matrix. Is there anyway to print the ordered list of rows/columns so that once can make a heatmap seaborn plot with given x and y labels?
Thanks,

Exclude indices from Pytorch tensor

I have an MxN table of distances between two distinct sets of points of size M <= N. I would like to find associate to each point of the first set M points in the second set in the following way.
Suppose that the shortest of all pairwise distances is between the i0 point of the first set and the j0 of the second. Then we attribute point i0 of the first set to j0 in the second. For the second pair, I have to find i1 != i0 and j1 != j0 such that the distance is minimal among remaining non-paired points.
I figure that I could do the first step by using torch.min function that will deliver me both minimal value as well as its 2d index in the matrix. But for the next steps I'll need to each time exclude a row a colunm, while keeping their original indices.
In other words, if I have a 3x4 matrix, and my first element is (1,2), I would like to be left with a 2x3 matrix with indices 0,2 and 0,1,3. So that, if my second desired element position in the original matrix is, say (2,3) I will be given (2,3) as a result of performing torch.max on the matrix with excluded row and column, rather than (1,2) again.
P.S. I could reach my goal by replacing the values in row and column I'd like to exclude by, say, positive infinities, but I think the question is still worth asking.

Can I compute the mean of two different axes of a 4D array using np.mean?

The data from my files is stored in 4D arrays in python of shape (64,128,64,3). The code I run is in a grid code format, so the shape tells us that there are 64 cells in the x,128 in the y, and 64 in the z. The 3 is the x, y, and z components of velocity. What I want to do is compute the average x velocity in each direction for every cell in y.
Let's start in the corner of my grid. I want the first element of my average array to be the average of the x velocity of all the x cells and all the z cells in position y[0]. The next element should be the same, but for y[1]. The end result should be an array of shape (128).
I'm fairly new to python, so I could be missing something simple, but I don't see a way to do this with one np.mean statement because you need to sum over two axes (In this case, 1 and 2 I think). I tried
velx_avg = np.mean(ds['u'][:,:,:,0],axis=1)
here, ds is the data set I've loaded in, and the module I've used to load it stores the velocity data under 'u'. This gave me an array of shape (64,64).
What is the most efficient way to produce the result that I want?
You can use the flatten command to make your life here much easier, this takes an np.ndarray and flattens it into one dimension.
The challenge here is trying to find your definition of 'efficient', but you can play around with that yourself. To do what you want, I simply iterate over the array and flatten the x and z component into a continuous array, and then take the mean of that, see below:
velx_avg = np.mean([ds['u'][:, i, :, 0].flatten() for i in range(128)], axis=1)

Extracting elements from matrix using strings subscript error

Hi I have a massive excel table and I want to extract values for specific compounds. Matrix repeats itself in 72 blocks, where vehicle-class is repeated with different road gradients and compounds and driving conditions.
cb=[Vehicleclass_raw,Gradient_raw,Component_raw];
size cb= 119952x3 cell
Vehicleclass_airquis is string with 37 elements
so I thought I sort it according to criteria gradient vehicle class and component and this is the code blow. it works for the first 30 elements and then it crashes and error message is Subscripted assignment dimension mismatch. outval_aq(i,:) = (rows). I cant figure out what the error is. Thanks for the help Matthias
outval_aq = ones(37,72)*119953;
for i=1:37
rows = find(strcmp(cb(:,1),Vehclass_airquis(i)) & strcmp(cb(:,2),'0%') & strcmp(cb(:,3),'NOx'));
if ~isempty(rows)
outval_aq(i,:) = (rows)
end
end
In a Matrix each row has the same number of elements. In your case you initialized outval_aq to be a 37x72 matrix but rows has only 60 elements. Solutions could look like:
%pad rows with nans to make it the required size:
rows(end+1:72)=nan
outval_aq(i,:) = (rows)
.
%write only the first 60 elements to the matrix, leave the remaining untouched:
outval_aq(i,1:numel(rows)) = (rows)
Or you could change outval_aq to be a cell array which contains vectors.

Probability question: Estimating the number of attempts needed to exhaustively try all possible placements in a word search

Would it be reasonable to systematically try all possible placements in a word search?
Grids commonly have dimensions of 15*15 (15 cells wide, 15 cells tall) and contain about 15 words to be placed, each of which can be placed in 8 possible directions. So in general it seems like you can calculate all possible placements by the following:
width*height*8_directions_to_place_word*number of words
So for such a grid it seems like we only need to try 15*15*8*15 = 27,000 which doesn't seem that bad at all. I am expecting some huge number so either the grid size and number of words is really small or there is something fishy with my math.
Formally speaking, assuming that x is number of rows and y is number of columns you should sum all the probabilities of every possible direction for every possible word.
Inputs are: x, y, l (average length of a word), n (total words)
so you have
horizontally a word can start from 0 to x-l and going right or from l to x going left for each row: 2x(x-l)
same approach is used for vertical words: they can go from 0 to y-l going down or from l to y going up. So it's 2y(y-l)
for diagonal words you shoul consider all possible start positions x*y and subtract l^2 since a rect of the field can't be used. As before you multiply by 4 since you have got 4 possible directions: 4*(x*y - l^2).
Then you multiply the whole result for the number of words included:
total = n*(2*x*(x-l)+2*y*(y-l)+4*(x*y-l^2)

Resources