Issue
I'm trying to create a Code which reads diferent matrices and evaluate if the have the dimensions that I want, i.e 882x883, If they do I want to add the matrices, if they donn't I want to attach each matrix as many columns and raws with Zeros as possible to achive These dimensions (for this we need to know that if they dont have the wanted dimensions the matrices have always smaller ones), the main Problem that I have is that the new files and rows need to be added in the same proportion at the beginning and at the end of the matrix, i.e. if I need to add 4 colums, two zero columns will be the first two columns and the other two the last two, if I need to add an odd number of rows or files then there will be one more at the beggining that at the end.
I will appreciate your help with this
Solution
You should take a look at
https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html
Its syntax is
numpy.pad(array, pad_width, mode, **kwargs)
If your matrix is named x
you get its dimensions via x.shape
. Then you can easily calculate how many rows or columns of zeros you have to attach to the matrix so it fits your needs. By specifying the pad_width
, which is a tuple of tuples of the form ((before_0, after_0), (before_1, after_1))
, containing the number of rows (axis 0) or columns (axis 1) you want to add to your matrix. Choose mode='constant'
and specify the constant values via constant_values=(0, 0)
which is a kwargs
argument.
So in total, something like
numpy.pad(x, ((before_0, after_0), (before_1, after_1)), 'constant', constant_values=(0, 0))
should do the job
Answered By - Solvalou
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.