How to select array elements which are within a range and get their indices?
Show older comments
Hi, I would like to get the value and index of elements from a cell array. For instant, I wanted to get the values and indices of the elements which falls within the range 0.95< x <1.15 from the row 'data{1,1}(:,5)'. Then I need to calculate the average of these elements.
I have thousands of elements in a row and I need to repeat the process for hundreds times. How do I do it efficiently?
Thank you in adv.
2 Comments
the cyclist
on 28 Aug 2017
Edited: the cyclist
on 28 Aug 2017
Note that your statement
0.95 < x < 0.15
would exclude all x. It looks like Image Analyst and I interpreted what you meant in different ways, so be careful about how you implement our code (if you do).
Jo 5
on 29 Aug 2017
Accepted Answer
More Answers (1)
the cyclist
on 28 Aug 2017
Edited: the cyclist
on 28 Aug 2017
I am not certain I understood exactly what you wanted, but this solution is probably close enough that you can figure it out:
% Make up some pretend data
rng default
C{1} = rand(1,50);
C{2} = rand(1,100);
% For each element of the cell array, calculated the mean of the vector elements that lie strictly within the range (0.15,0.95)
meanWithinRange = cellfun(@(x)mean(x(x>0.15&x<0.95)),C)
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!