How to filter a matrix?
18 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Stephen23
on 18 Apr 2016
Edited: Stephen23
on 18 Apr 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.
More Answers (1)
Azzi Abdelmalek
on 15 Apr 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
0 Comments
See Also
Categories
Find more on Categorical Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!