user3644826
user3644826

Reputation: 61

Line plot of rows in a Matlab matrix with the x-axis is the maximum value

I am new here and new to matlab.

I have a matrix file in matlab and what I want to do is make a plot of the average all the rows. However, I only want to plot a few data points (about 20) of the values before, and after, the maximum value within that row. The matrix file has 550 columns.

I have worked out how to identify the maximum value and the column number of this maximum value using;

[maxvalue maxindex] = max(filename, [], 2)

As the maximum is never in the same column, i really need help in calculating the average values for each row (before and afer the max value), and then how to plot this where the maximum value would be 0 on the x-axis.

For example - i have a matrix like this;

 14    51   623    23     4     1     4     5
 0     0     3     5    67    37    37     5
 0     0     0     0   574     4     5     6

and max value = 623 67 574

and max index = 3 5 5

So i would like to, plot the average of the 3 rows, 2 data points before and after the max values...so to plot the average of;

14, 51, 623, 23, 4, 1
3, 5, 67, 37, 37
0, 0, 574, 4, 5

Thanks so much for any help!

Upvotes: 0

Views: 172

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112689

data = [14    51   623    23     4     1     4     5
         0     0     3     5    67    37    37     5
         0     0     0     0   574     4     5     6]; %// example data
data = data.'; %'// it's easier to work along columns
[~, pos] = max(data); %// position of maxima
ind = bsxfun(@plus,bsxfun(@plus, pos,(-2:2).'),(0:size(data,2)-1)*size(data,1));
%'// linear index into the matrix obtained from pos
data_trimmed = data(ind).'; %'// index and transpose back
data = data.'; %'// undo transpose to put data back into shape

Result:

data_trimmed =

    14    51   623    23     4
     3     5    67    37    37
     0     0   574     4     5

Upvotes: 1

Related Questions