varfun question - How to create function handles and obtain function outputs

조회 수: 5 (최근 30일)
Hi all,
I am having trouble creating a function to be used on tables using the varfun function. I can obtain the min and max on the variables in a table (101x9 table) using the below code. However the main issue is I would also like to output the index at which these points occur and i can't figure out a way to implement the min or max function which gives you the output as well as index e.g. [M,I] = min(x).
I know you can get summary table information but I would like the index as well so I can calculate where this min or max occurs in the normalised data variables.
Any suggestions on if this is possible and if so how to do it would be greatly appreciated!
Thanks in advance
Jamie
%Find min and max of variables in table - Would like to also find index of these outputs
FindMin = @(x)(min(x));
FindMax = @(x)(max(x));
AllMin = varfun(FindMin,NormData);
AllMax = varfun(FindMax,NormData);
  댓글 수: 1
Jamie Hetherington
Jamie Hetherington 2017년 10월 8일
I have since simplified the previous code to what you see below, however still can't find a way to obtain index of each of the outputs. The below code put the min and max neatly next to each other in a table so wondering how its possible to write the indexing components into this anonymous function "FindMinMax"? If its even possible.
FindMinMax = @(x)[min(x),max(x)];
AAA = varfun(FindMinMax,NormData);

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

답변 (1개)

Francesco Onorati
Francesco Onorati 2020년 7월 7일
You can create a small function you can use in your function handle
function i = index_max_handle(x)
[~, i] = max(x);
end
Then
FindMax_ind = @(x)(index_max_handle(x));

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by