简体   繁体   English

从另一个数组的整数元素创建新数组? MATLAB

[英]Create new array from integer elements of another array? MATLAB

I have created an array tP which contains a mix of integer and non-integer elements. 我创建了一个数组tP ,其中包含整数和非整数元素的混合。 I want to create a new array of the integer element. 我想创建一个整数元素的新数组。

The result I would like is in the same form as is returned for, for example: 我想要的结果与返回的格式相同,例如:

tP2=find(tP>300);

That is, a list of the element numbers which contain integer values, not a list of the integers themselves. 即,包含整数值的元素编号列表,而不是整数本身的列表。

From this I will then select the desired elements like so: 然后,我将从中选择所需的元素,如下所示:

tP3=tP(tP2);

To do this for integers, what I currently have is: 为此,我目前拥有的是:

tP2=find(isinteger(int16(tP)));

But instead of a list of element numbers, I just get tP2=1 returned. 但是,我没有得到元素编号列表,而只是返回了tP2=1

Why does isinteger not work in this case and how can I achieve my required result? 为什么isinteger在这种情况下不起作用,如何获得所需的结果? Thanks. 谢谢。

使用round

tp2 = find( tP == round(tP) );

As Shai says, comparison to round is an effective way to detect integers. 正如Shai所说,与round比较是检测整数的有效方法。

Next, unless you need the list of matches for something else, you don't need find . 接下来,除非您需要其他匹配项列表,否则不需要find Just the comparison will create a mask array, and masks can be used for subscripting. 只是比较会创建一个掩码数组,而掩码可用于下标。

tP3 = tP(tP == round(tP));

Getting rid of tP2 and the call to find should save time and memory. 摆脱tP2find调用应该节省时间和内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM