0

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.

KLMac
  • 25
  • 3
  • Besides your question, take a look at logical indexing. You can also use `tP3=tP(tP>300)` – Daniel Mar 02 '14 at 20:35
  • I don't understand your question, an array (aka matrix) is either double or uint8 or any other type, but all elements have the same type. What data type is `tP2`? How is it created? – Daniel Mar 02 '14 at 20:40
  • 1
    `isinterger` refers to data type. A `double` for example can have an integer value and it will still be a `double`, not an integer data type – Luis Mendo Mar 02 '14 at 21:02
  • @Daniel: Well, a cell array can have elements with different data types. So in that case `isinteger` might return multiple values (I didn't bother to check if it can be used on cell arrays) – Ben Voigt Mar 02 '14 at 22:53

2 Answers2

1

use round

tp2 = find( tP == round(tP) );
Shai
  • 111,146
  • 38
  • 238
  • 371
0

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thank you for your input. I should have specified that I am using that list of element numbers for other arrays as well so will keep tP2, but I shall keep the tip in mind for the future. – KLMac Mar 02 '14 at 22:16