How to select array elements which are within a range and get their indices?

조회 수: 38 (최근 30일)
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
the cyclist
the cyclist 2017년 8월 28일
편집: the cyclist 2017년 8월 28일
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
Jo 5 2017년 8월 29일
Sorry typo! Should be 0.95 < x < 1.15. Appreciate your help!

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2017년 8월 28일
Try this:
% Get the whole row
row = data(...........whatever...
% Find values of row in the range "0.95< x <0.15"
% where x is either below 0.15 OR more than 0.95 but not in between.
logicalIndexes = row < 0.15 | row > 0.95;
% Get means where row is in range
meanValue = mean(row(logicalIndexes));
% You don't need the actual index numbers but if you want them, you can get them with find():
linearIndexes = find(logicalIndexes);
  댓글 수: 2
Image Analyst
Image Analyst 2017년 8월 29일
With your typo fixed, you'd change from OR to AND:
logicalIndexes = (row > 0.95) & (row < 1.15);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2017년 8월 28일
편집: the cyclist 2017년 8월 28일
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)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by