Converting an array slicer to Python

10 visualizaciones (últimos 30 días)
HC98
HC98 el 28 de En. de 2023
Respondida: sam el 17 de Abr. de 2023
I have this code in MATLAB (attached below) and wondered how I'd go about converting it to Python seeing as it keeps giving me an error "TypeError: only integer scalar arrays can be converted to a scalar index"
l0= -200
l0 = -200
hi = 20
hi = 20
x = ones(27000, 1);
udata = ones(27000, 10);
x1 = x((x>lo)&(x<hi));
Unrecognized function or variable 'lo'.
u1 = udata((x>lo)&(x<hi), :);

Respuesta aceptada

Chris
Chris el 28 de En. de 2023
Editada: Chris el 28 de En. de 2023
Here's a demonstration.
import numpy as np
lo = -200
hi = 20
x = np.ones((27000,1))
udata = np.ones((27000,10))
columnMask = (x>lo)&(x<hi)
cn = columnMask.nonzero()[0]
u1 = udata[cn,:]

Más respuestas (1)

sam
sam el 17 de Abr. de 2023
This error message suggests that a function or operation is expecting an integer scalar index (single integer) as an index, but is instead receiving an array or a non-integer value. This can occur when attempting to index or slice an array with a non-integer or non-scalar value. To resolve this error, ensure that the index being used is a single integer and not an array or non-integer value. If working with arrays, consider using integer indexing or slicing methods to access specific elements or subsets of the array.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by