How to access elements in numpy.array without converting the array?
조회 수: 14 (최근 30일)
이전 댓글 표시
Hello all,
I'm using an outofprocess python enviornment and would like to access individual or a set of elements in a numpy array without converting it. For example.
NP = py.importlib.import_module('numpy');
x = NP.array([1 3 5 7 9 11]);
now I would like to change x(2) to 0, this is what I do now and is clearly not efficient for very large arrays in a for loop for example.
tmp = double(x);
tmp(2) = 0;
x = NP.array(tmp);
my ultimate goal is to be able to take change a set of elements eg. 3:4, or 2:2:6
Thank you for your help
댓글 수: 0
채택된 답변
Al Danial
2023년 11월 3일
Use NumPy's put() function:
>> NP = py.importlib.import_module('numpy');
>> x = NP.array([1 3 5 7 9 11]);
>> ind = int64([0 2]); % indices to 1st and 3rd elements
>> newvals = [-11 -55]; % new values for 1st and 3rd elements
>> NP.put(x, ind, newvals);
>> x
x =
Python ndarray:
-11 3 -55 7 9 11
Use details function to view the properties of the Python object.
Use double function to convert to a MATLAB array.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!