I have created an array tP which contains a mix of integer and non-integer elements. 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.

Why does isinteger not work in this case and how can I achieve my required result? Thanks.

有帮助吗?

解决方案

use round

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

其他提示

As Shai says, comparison to round is an effective way to detect integers.

Next, unless you need the list of matches for something else, you don't need 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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top