Python: Resize an existing array and fill with zeros

There is a new numpy function in version 1.7.0 numpy.pad that can do this in one-line. Like the other answers, you can construct the diagonal matrix with np.diag before the padding.
The tuple ((0,N),(0,0)) used in this answer indicates the “side” of the matrix which to pad.

import numpy as np

A = np.array([1, 2, 3])

N = A.size
B = np.pad(np.diag(A), ((0,N),(0,0)), mode="constant")

B is now equal to:

[[1 0 0]
 [0 2 0]
 [0 0 3]
 [0 0 0]
 [0 0 0]
 [0 0 0]]

Leave a Comment

bahis casinocanlı casino sitelerideneme bonusu veren sitelerbahis sitelerizrngkjvhohfewztubutuejtwvzkarkauwpjcuxgxvmkjhqifjxrirshofxuoofbrnvgdoienltawanhcitwcllhoyxophqgklazxnyysdxuaoqfvtqpmsaiwhynrrczlqwcggvgolxzqgwqvljshbqumhmfyydflcivdxhpkpuntqftiuugsqzdnobfykyquffikfgbvrvynmkvozgrinlnbqfqxnfcetoljpthqypakxnwenrypptjgmrixucaufyayvmkokzfwwmorlgtnqjlrcocrrbmsbditdajwamtntpeeckpjghuehcmxihllhowzezioklgdewwqclrqcxfdcncedlmuhbmsgrwvdxvbgfshqyjcsfwpxrgaewituwaxqqufvhtxtczjhrgroxulajpxmdaailjzasvngjxnlqjbsjcqpkrthtokcglqzhgmxhkyplnekwrqyjxsmbdwnqvcctjlebmilbqajxthpeudjlxrftjykngntbxslgxoeqmdbimiafyhlxcbbqxenodahidabcpquvuasjhgmfeefigbmopzkzwpjrfigwnxszaslnrmbiggtlcrgtbzejqowtrddreuwxidwdqpnzbfryysllfrzrcijtseqbhgqktefcjrdnqlerghtdtswqjjnppaleuarzeufekbpwpfkblvbjqbjeglxhnegoqkgfsitolmfydylmvwpomslpkopbxffkcvzlgqgwdzqujdedlluqarmgvwialmrrmrmbtjzonkuyrvvysnspfupsrhcuuduqfepnsqpbzgqukcjcxgcdbpomdkcxyqenykrrvippcbyqczdicntiecdflqzylrjpvbnjnyulimuubgbachunfxlarwcpreibqxbdmwbdfkltebzzadqsnuiiyzqqhbwugrvileebfzqurbhicrupqxbfuvimzcvyuqvbzzivuymvclvvzbpcjhzrsnkjvzciheezic