create a matrix of maximums
4 views (last 30 days)
Hey guys, thanks in advance,
I have this code that correlates in time two signals. The correlation is presented in a matrix(range_compressed_matrix) with 1960(time_compression_cut) x 400(waypoints).
This matrix has 400 columns, and each column is filled with 1960 values of a signal in time domain. So the x of this matrix is a position of a plane=1:400. And the y of this matrix is 1960 values of time. Which means that for each column, or position of a plane, I have a value among that 1960 values that is a maximum.
I want to get the values(xmax ,ymax) where the correlation is maximum for each column. Meaning that I will have a xmax=400(position of plane) and ymax=400(time).
In the end I want to save this two variables in two matrixes, I have this code, but I m not getting what I want, and dont know why.
for col = 1 : columns
thisColumn = range_compressed_matrix(:, col);
maxValue = max(thisColumn(:));
[rows2, columns] = find(thisColumn == maxValue);
x_max(:,col) = waypoints(col);
y_max(rows,:) = time_compression_cut(rows2);
Voss on 5 Jul 2022
% get the maximum value of each column, along with the row it is located in
% (if multiple elements in a column are at the same maximum value, only the
% first occurrence of the maximum is stored):
[max_correlation, row_idx] = max(range_compressed_matrix, , 1);
% store the values of time_compression_cut where the max values occur:
y_max = time_compression_cut(row_idx);
% store the value of waypoints for each column:
x_max = waypoints;
More Answers (2)
Rohit Kulkarni on 5 Jul 2022
To get maximum value for each column and the corresponding indices you can do the following:
max_values = max(range_compressed_matrix)
%idx will give logical indices
idx = (range_compressed_matrix == max_values)
[rows, cols] = find(idx)
dpb on 5 Jul 2022
Edited: dpb on 5 Jul 2022
You didn't say there were values anywhere excepting in one column out of this array before -- you don't need no steenkin' loops here, either, max() is, like virtually all builtin MATLAB functions, vectorized and operates by column by default.
All you need is as I showed before is to return the optional second argument of max; here it'll give you the rows automagically because you're not looking for a global max across an entire array as in other...
>> r=rand(3,6) % some dummy data for your long-winded variable
0.0020 0.3362 0.6381 0.7472 0.0574 0.7932
0.3105 0.5159 0.9211 0.7499 0.0476 0.4048
0.1045 0.7225 0.8400 0.2613 0.0946 0.0230
>> [mx,rmx]=max(r) % get maxima by column, row location
0.3105 0.7225 0.9211 0.7499 0.0946 0.7932
2 3 2 2 3 1
The column is simply 1:size(r,2) of course.
The other arrays(?) in your code are undefined; we've no way to know what to do with those.