Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

3 demension matrix coding - find largest NN elements

1 vue (au cours des 30 derniers jours)
Hakjoon Yoon
Hakjoon Yoon le 8 Août 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
I have (M X N X sample) 3-demension matrix, and i want to change all elements to '0' except the largest 'NN' elements
for example;
when the matrix is (4 X 4 X 2) and NN=2
there are two matrix
1 2 3 4
1 2 1 2
2 1 1 2
and
4 1 2 3
1 2 1 2
1 2 2 1
and i want to change into
0 0 3 4
0 0 0 0
0 0 0 0
and
4 0 0 3
0 0 0 0
0 0 0 0
these two matrix
plz let me know how to change it....
  2 commentaires
Walter Roberson
Walter Roberson le 8 Août 2019
What do you want to do in the case of duplicates? For example if the original matrix were
1 2 3 4
1 2 1 2
3 1 1 2
then what would you want the output to be for NN=2 ?
Hakjoon Yoon
Hakjoon Yoon le 9 Août 2019
i want
0 0 3 4
0 0 0 0
0 0 0 0
or
0 0 0 4
0 0 0 0
3 0 0 0
only 'NN' elements are needed

Réponses (1)

Rik
Rik le 9 Août 2019
You will probably need to loop through the pages. The second output of max should be what you need.
A=rand(4,4,5);
out=A;
element_per_page=size(A, 1)*size(A,2);
for p=1:size(A,3)
[~,order]=sort(A(:,:,p));
ind=order(3:end)+element_per_page*(p-1);
out(ind)=0;
end
(untested code, written on mobile)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by