Extract datapoint corresponding to index from maximum of an array

I need to extract the force corresponding to the maximum strain for every cycle count from the attached file.
data=xlsread('Sample Data.xlsx');
count=data(:,1);
strain=data(:,2);
force=data(:,3);
unique_counts=unique(count);
maxforce=zeros(size(unique_counts));
maxstrain=zeros(size(unique_counts));
for n=1:numel(unique_counts)
c=unique_counts(n);
rows=ismember(count,c);
strain_=strain(rows);
force_=force(rows);
maxstrain(n)=max(strain_);
%% Here is where I need input
maxforce(n)= ?
end
Thank you

 Accepted Answer

The max() and min() functions can also return the index where the maximal/minimal value occurs. If I'm interpreting your code correctly, it would be something like this:
for n=1:numel(unique_counts)
c=unique_counts(n);
rows=ismember(count,c);
strain_=strain(rows);
force_=force(rows);
% get the index where the maximum occurred
[maxstrain(n) idx]=max(strain_);
maxforce(n)=force_(idx);
end

1 Comment

Thank you, your interpretation was correct :) minus a comma here:
[maxstrain(n),idx]=max(strain_);

Sign in to comment.

More Answers (0)

Categories

Asked:

on 23 Mar 2021

Commented:

on 23 Mar 2021

Community Treasure Hunt

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

Start Hunting!