Borrar filtros
Borrar filtros

Maximum value of matrix

2 visualizaciones (últimos 30 días)
mingcheng nie
mingcheng nie el 29 de Oct. de 2022
Comentada: mingcheng nie el 29 de Oct. de 2022
Hi there,
If we have a complex matrix A, size of (N,M), is there any efficient way to keep the maximum 6 absolute values in A and set others to be zeros? like abs(A(n,m)), keep the top6 biggest one, then set rest to 0.
Really appreciate for anyone leave a comment.

Respuesta aceptada

Kevin Holly
Kevin Holly el 29 de Oct. de 2022
% Generate random matrix
N = 34;
M = 23;
A = rand(N,M);
% Sort from highest to lowest
values = sort(abs(A(:)), 'descend')
values = 782×1
0.9998 0.9985 0.9980 0.9971 0.9965 0.9962 0.9950 0.9924 0.9914 0.9913
% top 6
values(1:6)
ans = 6×1
0.9998 0.9985 0.9980 0.9971 0.9965 0.9962
values(6)
ans = 0.9962
B = (A>=values(6)).*A
B = 34×23
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9980 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9985 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
imshow(B)
  3 comentarios
Kevin Holly
Kevin Holly el 29 de Oct. de 2022
I would appreciate it if you could accept this answer.
mingcheng nie
mingcheng nie el 29 de Oct. de 2022
sure definitely

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by