# Which part is incorrect?

3 vues (au cours des 30 derniers jours)
Ryan W le 15 Oct 2022
function [indices] = kWeakestRows(mat,k)% function
disp("The number of soldiers in each row is:")
for i = 1:length(mat)
fprintf("- Row %d: %d\n",i,sum(mat(i,:)));
end
indices = [];
for i = 1:k
end
fprintf("The rows ordered from weakest to strongest are ");
disp(indices);
end
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Ghazwan le 15 Oct 2022
can you explain more?

Connectez-vous pour commenter.

### Réponses (1)

Hiro Yoshino le 15 Oct 2022
I would do this much more simply:
mat = [1,1,0,0,0;
1,1,1,1,0;
1,0,0,0,0;
1,1,0,0,0;
1,1,1,1,1];
% sum in row direction
2 4 1 2 5
% sort and obtain the indices
disp("The rows ordered from weakest to strongest are")
The rows ordered from weakest to strongest are
idx
idx = 5×1
3 1 4 2 5
If you want to extract first k then:
k = 3;
idx(1:k)
ans = 3×1
3 1 4
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by