How to filter a matrix?

23 visualizaciones (últimos 30 días)
Lucas Black
Lucas Black el 15 de Abr. de 2016
Editada: Stephen23 el 18 de Abr. de 2016
I'm clustering my data with the aim to produce a force directed graph. I've got the script running but the graphs are too connected and in need of filtering. As I produce the charts from the pdist function I want to filter out all but the top 10 values per row so that each node has no more than 10 edges in the resulting graph.
[M, Idx] = max(A,[],2)
gives me the max value and the location along the row but I'm unsure how to find the top 10 positions for each row or how to convert that back into an array of the same size as A
  2 comentarios
Stephen23
Stephen23 el 15 de Abr. de 2016
Editada: Stephen23 el 15 de Abr. de 2016
How would the output be "an array of the same size as A"? Surely by "filtering out" the lesser values you have made your matrix smaller. How can it be the same size?
Lucas Black
Lucas Black el 18 de Abr. de 2016
Hi Stephen,
Sorry if I used the wrong terms. By filter out I mean keep the top ten values but replace all the other valued with 0.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 18 de Abr. de 2016
Editada: Stephen23 el 18 de Abr. de 2016
A = randi(9,6,20) % example matrix of integers
N = 10; % pick how many values to keep
[B,C] = sort(A,2);
B(:,1:end-N) = 0;
R = (1:size(B,1))'*ones(1,size(B,2));
Z = zeros(size(B));
Z(sub2ind(size(B),R,C)) = B % output
Where the example input and output matrix is:
A =
5 6 7 4 6 5 5 9 3 2 5 7 5 2 1 8 4 9 5 4
9 3 8 5 7 4 3 2 5 5 8 5 3 6 5 2 9 6 4 7
5 9 5 9 3 8 3 5 2 3 8 4 2 9 1 5 4 6 5 9
4 6 8 7 5 7 3 4 5 1 7 6 8 7 8 5 8 6 6 6
3 2 7 5 6 1 8 4 8 6 2 2 5 8 5 9 9 1 5 2
7 8 6 6 1 4 3 1 5 3 5 7 7 7 8 7 6 6 7 9
Z =
0 6 7 0 6 0 0 9 0 0 5 7 5 0 0 8 0 9 5 0
9 0 8 0 7 0 0 0 0 0 8 5 0 6 5 0 9 6 0 7
0 9 0 9 0 8 0 5 0 0 8 0 0 9 0 5 0 6 5 9
0 0 8 7 0 7 0 0 0 0 7 0 8 7 8 0 8 0 6 6
0 0 7 0 6 0 8 0 8 6 0 0 0 8 5 9 9 0 5 0
7 8 0 0 0 0 0 0 0 0 0 7 7 7 8 7 0 6 7 9
Note that picking the largest ten values may have unintended side effects: the code above picks the first of identical values, which means although one value might exist in multiple locations in one row, it is possible that only the first ones get returned (e.g 5 in the first row: there are actually six of them in the original matrix, but only three in the output). By "filtering" based on value rather than number of occurrences you could avoid this problem.
  1 comentario
Lucas Black
Lucas Black el 18 de Abr. de 2016
Thanks Stephen, that worked a treat :D

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 15 de Abr. de 2016
A=randi(80,4,20)
n=size(A,1)
max1=zeros(n,10);
indices=zeros(n,10);
for k=1:size(A,1)
[ii,jj]=sort(A(k,:),'descend');
max1(k,:)=ii(1:10);
indices(k,:)=jj(1:10);
end

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by