[英]Numpy: Create new array from 1d integer array where each element in integer array represents the number of elements in the new array
[英]Create new array from integer elements of another array? MATLAB
我創建了一個數組tP
,其中包含整數和非整數元素的混合。 我想創建一個整數元素的新數組。
我想要的結果與返回的格式相同,例如:
tP2=find(tP>300);
即,包含整數值的元素編號列表,而不是整數本身的列表。
然后,我將從中選擇所需的元素,如下所示:
tP3=tP(tP2);
為此,我目前擁有的是:
tP2=find(isinteger(int16(tP)));
但是,我沒有得到元素編號列表,而只是返回了tP2=1
。
為什么isinteger
在這種情況下不起作用,如何獲得所需的結果? 謝謝。
使用round
tp2 = find( tP == round(tP) );
正如Shai所說,與round
比較是檢測整數的有效方法。
接下來,除非您需要其他匹配項列表,否則不需要find
。 只是比較會創建一個掩碼數組,而掩碼可用於下標。
tP3 = tP(tP == round(tP));
擺脫tP2
和find
調用應該節省時間和內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.