Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Could anyone please help me to code this problem.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (4)
Andrei Bobrov
am 4 Mai 2017
Bearbeitet: Andrei Bobrov
am 4 Mai 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 Kommentare
Stephen23
am 4 Mai 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 Kommentare
Jan
am 4 Mai 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 Kommentare
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!