フィルターのクリア

Is it a Python matlab.engine's bug for slicing 2D data?

2 ビュー (過去 30 日間)
友宝 张
友宝 张 2024 年 4 月 7 日
回答済み: Lokesh 2024 年 4 月 23 日
The code is as following, when we slicing 2D data img, we've gotten unexpected result. The code is straightforward.
import matlab.engine
import matlab
img = matlab.uint8([[14,18,18,22,24,26,27,31,35,40,42,46,50,51,50,51,43,43,40,35,30,26,24,22,18],[18,19,18,24,26,30,38,43,50,58,62,70,72,78,74,72,66,58,50,43,40,34,26,26,22],[18,22,24,27,34,42,48,60,74,86,104,120,127,130,126,120,104,86,74,62,50,42,38,30,26],[22,26,26,35,46,56,67,91,118,146,174,188,200,209,199,192,167,147,112,88,70,56,43,38,27],[24,30,38,46,59,82,111,151,191,222,245,255,255,255,255,255,244,222,186,142,106,76,58,46,38],[26,34,46,58,82,116,168,221,247,255,255,255,255,255,255,255,255,255,243,202,163,112,78,56,42],[34,42,56,76,120,173,227,255,255,255,255,255,255,255,255,255,255,255,255,255,219,162,111,74,54],[40,50,67,106,170,227,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,221,155,98,66],[46,62,90,142,211,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,202,134,82],[50,70,108,184,252,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,235,165,102],[56,78,132,214,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,198,122],[59,92,155,233,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,219,142],[66,99,171,248,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,238,158],[67,106,182,254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,243,164],[66,104,180,254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,241,170],[66,104,177,248,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,243,159],[62,94,162,241,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,227,154],[58,86,140,220,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,208,130],[51,74,122,190,254,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,248,179,110],[46,67,98,156,222,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,212,142,88],[42,54,78,126,180,240,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,233,171,112,72],[35,43,63,90,130,186,237,255,255,255,255,255,255,255,255,255,255,255,255,255,233,174,124,78,59],[31,38,50,66,95,142,188,240,255,255,255,255,255,255,255,255,255,255,255,225,174,130,88,62,46],[26,31,42,51,70,92,127,172,210,242,255,255,255,255,255,255,248,234,200,163,120,88,63,51,40],[26,27,31,42,54,64,83,110,146,166,200,209,220,225,218,207,192,163,132,102,78,62,54,42,31]]);
print(img.size) # (25, 25), it is expected.
sub_img = img[0:14][0:14];
print(sub_img.size) # (15, 15) is expected. However, we've gotten (14, 25)

回答 (1 件)

Lokesh
Lokesh 2024 年 4 月 23 日
Hi,
I understand that the 'sub_img' obtained after slicing 2D data 'img' does not match your expected result.
When attempting to slice a 2D array in Python, and specifically a 2D MATLAB array through Python in this case, the syntax 'img[0:14][0:14]' performs the operation in two steps. Initially, 'img[0:14]' slices the array to include rows 0 through 13, as Python slicing is zero-based and the end index is exclusive. Subsequently, applying [0:14] to the result of 'img[0:14]' slices this intermediate array by rows once again, not columns, resulting in an array of size (14,25).
To correctly slice a 2D array to get a sub-array, you should use a single slicing operation that includes both rows and columns. Here is a sample code:
import matlab.engine
import matlab
# Start MATLAB engine
eng = matlab.engine.start_matlab()
# Create a sample MATLAB array
img = matlab.uint8([[14,18,18,22,24,26,27,...]])
# Assign the MATLAB array to the 'img' variable in the MATLAB workspace
eng.workspace['img'] = img
# Use MATLAB's indexing (which starts at 1) and includes the end index to slice the array
sub_img = eng.eval('img(1:15, 1:15)') # This should give you the 15x15 sub-array
print(sub_img.size) # Should print the size of sub_img, expecting [15, 15]
Hope this helps!

カテゴリ

Help Center および File ExchangeCall MATLAB from Python についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by