Usando o método numpy.fill_diagonal()
, você pode preencher as diagonais de um array numpy com o valor passado como parâmetro para numpy.fill_diagonal ( )
.
Sintaxe: numpy.fill_diagonal (matriz, valor)
Retorno: Retorna o preenchido valor na diagonal de uma matriz.
Exemplo # 1:
Neste exemplo, podemos ver que usando numpy.fill_diagonal ( )
podemos obter as diagonais preenchidas com os valores passados como parâmetro.
# NumPy import import numpy as np # usando o método numpy.fill_diagonal() array = np.array ([[ 1 , 2 ], [ 2 , 1 ]]) np .fill_diagonal (array, 5 ) print (array) |
Saída:
[[5 2]
[2 5]]
Exemplo nº 2:
< /p>
# NumPy import import numpy as np < br /> # usando o método numpy.fill_diagonal() array = np.zeros (( 3 , 3 ), int ) np.fill_diagonal (array, 1 ) print (array) |
Saída:
[ [1 0 0]
[0 1 0]
[0 0 1]]
Esperamos que este artigo tenha ajudado você a resolver o problema. Além de Python | método numpy.fill_diagonal(), confira outros tópicos relacionados a Python functions.
Quer se destacar em Python? Veja nossa análise dosmelhores cursos on-line de Python 2023. Se você estiver interessado em Ciência de Dados, veja também como aprender programação em R.
A propósito, este material também está disponível em outros idiomas:
Oliver Krasiko
Vigrinia | 2023-03-25
De forma simples e clara. Obrigado por compartilhar. Python | método numpy.fill_diagonal() e outras questões com Python functions sempre foi meu ponto fraco 😁. Vou usá-lo na minha tese de bacharelado
Jan Emmerson
Prague | 2023-03-25
Talvez haja outra resposta? O que Python | método numpy.fill_diagonal() significa exatamente?. Verificado ontem, funciona!
Julia Jackson
Massachussetts | 2023-03-25
Obrigado por explicar! Eu fiquei preso com Python | método numpy.fill_diagonal() por algumas horas, finalmente consegui fazer isso 🤗. Vou usá-lo na minha tese de bacharelado
Shop
Learn programming in R: courses
$FREE
Best Python online courses for 2022
$FREE
Best laptop for Fortnite
$399+
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically