Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Could anyone please help me to code this problem.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a matrix
M=[1
2
3
5
7
7
9
10
11
11
12
13
16
16
22
22
23
23
23
44
45
65
103
113]
and another matrix
maxtop= [7
12
24
]
I want to get the top values(maximum) from matrix M that means, for the first time, I want to take the first value in maxtop ie,7 and find the top 7 values (along with duplication) from matrix M and store it in another variable Mst ie, 113,103,65,45,44,23,23 and then take 12 in matrix maxtop and find the top 12 values and so on. Thanks in advance.
2 comentarios
Respuestas (4)
KL
el 4 de Mayo de 2017
M_sorted = sort(M,'descend');
M_maxtop = M_sorted(1:maxtop(1));
0 comentarios
Andrei Bobrov
el 4 de Mayo de 2017
Editada: Andrei Bobrov
el 4 de Mayo de 2017
M1 = flipud(M(:));
out = cell2mat(arrayfun(@(x)M1(1:x),maxtop(:),'un',0));
or
M1 = flipud(M);
m = numel(M1);
n = numel(maxtop);
ii = zeros(m+1,n);
ii(1,:) = 1;
ii(maxtop + (m+1).*(0:n-1)'+1) = -1;
out = nonzeros(bsxfun(@times,M1,cumsum(ii(1:end-1,:))));
or
k = [0;cumsum(maxtop(:))];
out1 = zeros(k(end),1);
for ii = 1:numel(maxtop)
out1(k(ii)+1:k(ii + 1)) = M(end:-1:(end - maxtop(ii) + 1));
end
0 comentarios
Stephen23
el 4 de Mayo de 2017
Just use arrayfun:
>> C = arrayfun(@(n)M(1:n),maxtop,'uni',0);
>> C{:}
ans =
1
2
3
5
7
7
9
ans =
1
2
3
5
7
7
9
10
11
11
12
13
ans =
1
2
3
5
7
7
9
10
11
11
12
13
16
16
22
22
23
23
23
44
45
65
103
113
0 comentarios
Jan
el 4 de Mayo de 2017
M = [1;2;3;5;7;7;9;10;11;11;12;13;16;16;22;22;23;23;23;44;45;65;103;113];
maxtop = [7, 12, 24];
Result = cell(1, numel(maxtop));
sM = sort(M, 'descend');
for k = 1:numel(maxtop)
Result{k} = sM(1:maxtop(k));
end
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!