How to obtain the indexes of numbers in a cell array with mixed data?

6 views (last 30 days)
I have different cell arrays like:
cellArray1={'PGD',[23095],[5226],[NaN],[NaN]};
cellArray2={[12342],[NaN],[NaN],[NaN],[NaN]}
cellArray3={'A1','APIT','CRT',[378708],[100526739]}
and I would like to obtain the indexes of the numbers in the array such as:
indexes1= 2,3
indexes2= 1
indexes3= 4,5
respectively
Cheers

Accepted Answer

Cam Salzberger
Cam Salzberger on 17 May 2017
You can use "cellfun" to run the is* style functions for every cell in a cell array. For your particular use-case, you seem to want only cells that are numeric and non-NaN. You could do two calls to cellfun, or just make an anonymous function that checks both:
cellfun(@(c) isnumeric(c) && ~isnan(c),cellArray1)
This will give you a logical array the same size as the cell array. You can choose to use "find" if absolutely necessary to get the indices out, but that usually isn't required.
-Cam

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!