How to find minimum from a group and index at which minimum value is obtained ?

조회 수: 1 (최근 30일)
Hello all,
I have a matrix Engine_data_temp whose 5th column contains values of time (eg: 1;1;1;2;2...600) and 10th column contains values of fuel consumption. I am currently using code shown below to find unique value of time and minimum value of fuel consumed at that time:
(As suggested my one of the community member)
[uniqueT,~,jk]=unique(Engine_data_temp(:,5));
minFc=accumarray(jk,Engine_data_temp(:,10),[],@min);
I also need to find the index at which min values of fuel consumption are being selected at each unique time instances because I need this index to access other columns in matrix Engine_data_temp.
Thank you!
  댓글 수: 1
Dhaval Lodaya
Dhaval Lodaya 2016년 7월 3일
I am looking for a method which does not use for loop for this as it slows down my code. I need to obtain index by vectorization or any other method that is fast.
Thanks in advance!

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

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 3일
Create a small function
function r = min_and_idx(x)
[minx, minidx] = min(x);
r = {minx, minidx};
Then you use
minFc = accumarray(jk, Engine_data_temp(:,10), [], @min_and_idx, {});
The result will be a cell array in which some entries might be empty, but if they are not empty then they will be a cell with minimum value and the index of the value relative to the locations that share the same jk value.
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 7월 4일
function r = min_and_idx(x)
[minx, minidx] = min(x);
r = {[minx, minidx]};
Dhaval Lodaya
Dhaval Lodaya 2016년 7월 4일
편집: Dhaval Lodaya 2016년 7월 4일
Thanks a lot! This works perfectly fine now and I liked the cell approach.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by