Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Could anyone please help me to code this problem.

1 visualizzazione (ultimi 30 giorni)
SUNANNA S S
SUNANNA S S il 4 Mag 2017
Chiuso: MATLAB Answer Bot il 20 Ago 2021
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 Commenti
KSSV
KSSV il 4 Mag 2017
Question not clear....you want to save first/top seven..and then?
SUNANNA S S
SUNANNA S S il 4 Mag 2017
I want the top values.The number of top values I want to take from matrix M is based on the values in maxtop, ie,first take 7 and find the top 7 values and then take 12 and find the top 12 and then 24 and take the top 24 values from matrix M .

Risposte (4)

KL
KL il 4 Mag 2017
M_sorted = sort(M,'descend');
M_maxtop = M_sorted(1:maxtop(1));

Andrei Bobrov
Andrei Bobrov il 4 Mag 2017
Modificato: Andrei Bobrov il 4 Mag 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

Stephen23
Stephen23 il 4 Mag 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

Jan
Jan il 4 Mag 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

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by