Borrar filtros
Borrar filtros

create a matrix of maximums

4 visualizaciones (últimos 30 días)
Miguel Albuquerque
Miguel Albuquerque el 5 de Jul. de 2022
Editada: dpb el 5 de Jul. de 2022
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.
[rows,columns]=size(range_compressed_matrix);
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);
y_max(rows,:)= y_max(:,col)*c;
end
  3 comentarios
Miguel Albuquerque
Miguel Albuquerque el 5 de Jul. de 2022
its the speed of light
Johan
Johan el 5 de Jul. de 2022
It's not very clear to me what you want to achieve here, a clearer explaination might help you and us solving your problem. I put some comments in the code snippets you shared:
%Random values
range_compressed_matrix = rand(1960,400);
waypoints = rand(400,1);
time_compression_cut = rand(1960,1);
[rows,columns]=size(range_compressed_matrix)
rows = 1960
columns = 400
c = 1;
for col = 1 : columns
thisColumn = range_compressed_matrix(:, col);
maxValue = max(thisColumn(:));
%You are setting columns as an output, its already used as your loop limit,
%moreover your not using that variable so you can replace it by ~
% [rows2, columns] = find(thisColumn == maxValue);
[rows2, ~] = find(thisColumn == maxValue);
%This is equivalent to x_max = waypoints'
x_max(:,col) = waypoints(col);
%rows = 1960 so this line sets the 1960 value of y_max to time_compression_cut(rows2);
y_max(rows,:) = time_compression_cut(rows2);
%y_max is single columned so this doesn't work
% y_max(rows,:)= y_max(:,col)*c;
end
all(x_max == waypoints')
ans = logical
1

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 5 de Jul. de 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;

Más respuestas (2)

Rohit Kulkarni
Rohit Kulkarni el 5 de Jul. de 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)
  1 comentario
dpb
dpb el 5 de Jul. de 2022
Editada: dpb el 5 de Jul. de 2022
More direct is simply
[max_values,rows] = max(range_compressed_matrix);
cols=1:size(range_compressed_matrix,2);
You've already searched the array once to find the maxima, why on earth search it again to locate what has already been located????
find is rarely needed; as here, when not needed specifically, it's just wasted motion.

Iniciar sesión para comentar.


dpb
dpb el 5 de Jul. de 2022
Editada: dpb el 5 de Jul. de 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...
Example:
>> r=rand(3,6) % some dummy data for your long-winded variable
r =
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
mx =
0.3105 0.7225 0.9211 0.7499 0.0946 0.7932
rmx =
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.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by