Info
此问题已关闭。 请重新打开它进行编辑或回答。
Could anyone please help me to code this problem.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(4 个)
Andrei Bobrov
2017-5-4
编辑:Andrei Bobrov
2017-5-4
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 个评论
Stephen23
2017-5-4
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 个评论
Jan
2017-5-4
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 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!