문제

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